欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

歡迎來到入門教程網(wǎng)!

C#教程

當(dāng)前位置:主頁 > 軟件編程 > C#教程 >

C#利用SFTP實(shí)現(xiàn)上傳下載

來源:本站原創(chuàng)|時(shí)間:2020-01-10|欄目:C#教程|點(diǎn)擊: 次

sftp是ftp協(xié)議的升級版本,是犧牲上傳速度為代價(jià),換取安全性能,本人開始嘗試使用Tamir.SharpSSH.dll但它對新版本的openssh 不支持,所有采用Ssh.Net方式 需要依賴:Renci.SshNet.dll 下載鏈接

/// <summary>
  /// SFTP操作類
  /// </summary>
  public class SFTPHelper
  {
    #region 字段或?qū)傩?
    private SftpClient sftp;
    /// <summary>
    /// SFTP連接狀態(tài)
    /// </summary>
    public bool Connected { get { return sftp.IsConnected; } }
    #endregion

    #region 構(gòu)造
    /// <summary>
    /// 構(gòu)造
    /// </summary>
    /// <param name="ip">IP</param>
    /// <param name="port">端口</param>
    /// <param name="user">用戶名</param>
    /// <param name="pwd">密碼</param>
    public SFTPHelper(string ip, string port, string user, string pwd)
    {
      sftp = new SftpClient(ip, Int32.Parse(port), user, pwd);
    }
    #endregion

    #region 連接SFTP
    /// <summary>
    /// 連接SFTP
    /// </summary>
    /// <returns>true成功</returns>
    public bool Connect()
    {
      try
      {
        if (!Connected)
        {
          sftp.Connect();
        }
        return true;
      }
      catch (Exception ex)
      {
        // TxtLog.WriteTxt(CommonMethod.GetProgramName(), string.Format("連接SFTP失敗,原因:{0}", ex.Message));
        throw new Exception(string.Format("連接SFTP失敗,原因:{0}", ex.Message));
      }
    }
    #endregion

    #region 斷開SFTP
    /// <summary>
    /// 斷開SFTP
    /// </summary> 
    public void Disconnect()
    {
      try
      {
        if (sftp != null && Connected)
        {
          sftp.Disconnect();
        }
      }
      catch (Exception ex)
      {
        // TxtLog.WriteTxt(CommonMethod.GetProgramName(), string.Format("斷開SFTP失敗,原因:{0}", ex.Message));
        throw new Exception(string.Format("斷開SFTP失敗,原因:{0}", ex.Message));
      }
    }
    #endregion

    #region SFTP上傳文件
    /// <summary>
    /// SFTP上傳文件
    /// </summary>
    /// <param name="localPath">本地路徑</param>
    /// <param name="remotePath">遠(yuǎn)程路徑</param>
    public void Put(string localPath, string remotePath)
    {
      try
      {
        using (var file = File.OpenRead(localPath))
        {
          Connect();
          sftp.UploadFile(file, remotePath);
          Disconnect();
        }
      }
      catch (Exception ex)
      {
        // TxtLog.WriteTxt(CommonMethod.GetProgramName(), string.Format("SFTP文件上傳失敗,原因:{0}", ex.Message));
        throw new Exception(string.Format("SFTP文件上傳失敗,原因:{0}", ex.Message));
      }
    }
    #endregion

    #region SFTP獲取文件
    /// <summary>
    /// SFTP獲取文件
    /// </summary>
    /// <param name="remotePath">遠(yuǎn)程路徑</param>
    /// <param name="localPath">本地路徑</param>
    public void Get(string remotePath, string localPath)
    {
      try
      {
        Connect();
        var byt = sftp.ReadAllBytes(remotePath);
        Disconnect();
        File.WriteAllBytes(localPath, byt);
      }
      catch (Exception ex)
      {
        // TxtLog.WriteTxt(CommonMethod.GetProgramName(), string.Format("SFTP文件獲取失敗,原因:{0}", ex.Message));
        throw new Exception(string.Format("SFTP文件獲取失敗,原因:{0}", ex.Message));
      }

    }
    #endregion

    #region 刪除SFTP文件
    /// <summary>
    /// 刪除SFTP文件 
    /// </summary>
    /// <param name="remoteFile">遠(yuǎn)程路徑</param>
    public void Delete(string remoteFile)
    {
      try
      {
        Connect();
        sftp.Delete(remoteFile);
        Disconnect();
      }
      catch (Exception ex)
      {
        // TxtLog.WriteTxt(CommonMethod.GetProgramName(), string.Format("SFTP文件刪除失敗,原因:{0}", ex.Message));
        throw new Exception(string.Format("SFTP文件刪除失敗,原因:{0}", ex.Message));
      }
    }
    #endregion

    #region 獲取SFTP文件列表
    /// <summary>
    /// 獲取SFTP文件列表
    /// </summary>
    /// <param name="remotePath">遠(yuǎn)程目錄</param>
    /// <param name="fileSuffix">文件后綴</param>
    /// <returns></returns>
    public ArrayList GetFileList(string remotePath, string fileSuffix)
    {
      try
      {
        Connect();
        var files = sftp.ListDirectory(remotePath);
        Disconnect();
        var objList = new ArrayList();
        foreach (var file in files)
        {
          string name = file.Name;
          if (name.Length > (fileSuffix.Length + 1) && fileSuffix == name.Substring(name.Length - fileSuffix.Length))
          {
            objList.Add(name);
          }
        }
        return objList;
      }
      catch (Exception ex)
      {
        // TxtLog.WriteTxt(CommonMethod.GetProgramName(), string.Format("SFTP文件列表獲取失敗,原因:{0}", ex.Message));
        throw new Exception(string.Format("SFTP文件列表獲取失敗,原因:{0}", ex.Message));
      }
    }
    #endregion

    #region 移動SFTP文件
    /// <summary>
    /// 移動SFTP文件
    /// </summary>
    /// <param name="oldRemotePath">舊遠(yuǎn)程路徑</param>
    /// <param name="newRemotePath">新遠(yuǎn)程路徑</param>
    public void Move(string oldRemotePath, string newRemotePath)
    {
      try
      {
        Connect();
        sftp.RenameFile(oldRemotePath, newRemotePath);
        Disconnect();
      }
      catch (Exception ex)
      {
        // TxtLog.WriteTxt(CommonMethod.GetProgramName(), string.Format("SFTP文件移動失敗,原因:{0}", ex.Message));
        throw new Exception(string.Format("SFTP文件移動失敗,原因:{0}", ex.Message));
      }
    }
    #endregion

  }

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。

上一篇:C#實(shí)現(xiàn)備忘錄功能

欄    目:C#教程

下一篇:C# 操作PostgreSQL 數(shù)據(jù)庫的示例代碼

本文標(biāo)題:C#利用SFTP實(shí)現(xiàn)上傳下載

本文地址:http://mengdiqiu.com.cn/a1/C_jiaocheng/5426.html

網(wǎng)頁制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語言數(shù)據(jù)庫服務(wù)器

如果侵犯了您的權(quán)利,請與我們聯(lián)系,我們將在24小時(shí)內(nèi)進(jìn)行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負(fù)任何責(zé)任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有