C#利用SFTP實現(xiàn)上傳下載-創(chuàng)新互聯(lián)

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

創(chuàng)新互聯(lián)建站于2013年創(chuàng)立,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項目網(wǎng)站設(shè)計制作、成都做網(wǎng)站網(wǎng)站策劃,項目實施與項目整合能力。我們以讓每一個夢想脫穎而出為使命,1280元東鄉(xiāng)做網(wǎng)站,已為上家服務(wù),為東鄉(xiāng)各地企業(yè)和個人服務(wù),聯(lián)系電話:18980820575
/// <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

  }

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。

網(wǎng)頁題目:C#利用SFTP實現(xiàn)上傳下載-創(chuàng)新互聯(lián)
本文鏈接:http://bm7419.com/article36/hscsg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供定制開發(fā)響應(yīng)式網(wǎng)站網(wǎng)站設(shè)計公司、網(wǎng)站排名全網(wǎng)營銷推廣、微信小程序

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)