c# 操作 Sftp

先在項目中引用  Renci.SshNet.dll (點擊下載)

創建 SFTPHelper.cs 類

代碼:

using Renci.SshNet;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CheatingTransmitterCalibration
{
    /*
     * 此類爲sftp連接操作類
     * 使用方法:引用SshNet文件夾下的Renci.SshNet.dll文件,以及此類文件,即可使用
     */
    public class SFTPOperation
    {
        #region 字段或屬性
        private SftpClient sftp;
        /// <summary>
        /// SFTP連接狀態
        /// </summary>
        public bool Connected { get { return sftp.IsConnected; } }
        #endregion

        #region 構造
        /// <summary>
        /// 構造
        /// </summary>
        /// <param name="ip">IP</param>
        /// <param name="port">端口</param>
        /// <param name="user">用戶名</param>
        /// <param name="pwd">密碼</param>
        public SFTPOperation(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)
            {
                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)
            {
                throw new Exception(string.Format("斷開SFTP失敗,原因:{0}", ex.Message));
            }
        }
        #endregion

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

        #region SFTP獲取文件
        /// <summary>
        /// SFTP獲取文件
        /// </summary>
        /// <param name="remotePath">遠程路徑</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)
            {
                throw new Exception(string.Format("SFTP文件獲取失敗,原因:{0}", ex.Message));
            }

        }
        #endregion

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

        #region 獲取SFTP文件列表
        /// <summary>
        /// 獲取SFTP文件列表
        /// </summary>
        /// <param name="remotePath">遠程目錄</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)
            {
                throw new Exception(string.Format("SFTP文件列表獲取失敗,原因:{0}", ex.Message));
            }
        }
        #endregion

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

    }
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章