SFTP上傳下載(C#)

sftp是ftp協議的升級版本,是犧牲上傳速度爲代價,換取安全性能,本人開始嘗試使用Tamir.SharpSSH.dll但它對新版本的openssh 不支持,全部採用Ssh.Net方式 須要依賴:Renci.SshNet.dll 連接: http://pan.baidu.com/s/1nvuHFVN 密碼: hh49安全

  1     /// <summary>
  2     /// SFTP操做類
  3     /// </summary>
  4     public class SFTPHelper
  5     {
  6         #region 字段或屬性
  7         private SftpClient sftp;
  8         /// <summary>
  9         /// SFTP鏈接狀態
 10         /// </summary>
 11         public bool Connected { get { return sftp.IsConnected; } }
 12         #endregion
 13 
 14         #region 構造
 15         /// <summary>
 16         /// 構造
 17         /// </summary>
 18         /// <param name="ip">IP</param>
 19         /// <param name="port">端口</param>
 20         /// <param name="user">用戶名</param>
 21         /// <param name="pwd">密碼</param>
 22         public SFTPHelper(string ip, string port, string user, string pwd)
 23         {
 24             sftp = new SftpClient(ip, Int32.Parse(port), user, pwd);
 25         }
 26         #endregion
 27 
 28         #region 鏈接SFTP
 29         /// <summary>
 30         /// 鏈接SFTP
 31         /// </summary>
 32         /// <returns>true成功</returns>
 33         public bool Connect()
 34         {
 35             try
 36             {
 37                 if (!Connected)
 38                 {
 39                     sftp.Connect();
 40                 }
 41                 return true;
 42             }
 43             catch (Exception ex)
 44             {
 45                 // TxtLog.WriteTxt(CommonMethod.GetProgramName(), string.Format("鏈接SFTP失敗,緣由:{0}", ex.Message));
 46                 throw new Exception(string.Format("鏈接SFTP失敗,緣由:{0}", ex.Message));
 47             }
 48         }
 49         #endregion
 50 
 51         #region 斷開SFTP
 52         /// <summary>
 53         /// 斷開SFTP
 54         /// </summary> 
 55         public void Disconnect()
 56         {
 57             try
 58             {
 59                 if (sftp != null && Connected)
 60                 {
 61                     sftp.Disconnect();
 62                 }
 63             }
 64             catch (Exception ex)
 65             {
 66                 // TxtLog.WriteTxt(CommonMethod.GetProgramName(), string.Format("斷開SFTP失敗,緣由:{0}", ex.Message));
 67                 throw new Exception(string.Format("斷開SFTP失敗,緣由:{0}", ex.Message));
 68             }
 69         }
 70         #endregion
 71 
 72         #region SFTP上傳文件
 73         /// <summary>
 74         /// SFTP上傳文件
 75         /// </summary>
 76         /// <param name="localPath">本地路徑</param>
 77         /// <param name="remotePath">遠程路徑</param>
 78         public void Put(string localPath, string remotePath)
 79         {
 80             try
 81             {
 82                 using (var file = File.OpenRead(localPath))
 83                 {
 84                     Connect();
 85                     sftp.UploadFile(file, remotePath);
 86                     Disconnect();
 87                 }
 88             }
 89             catch (Exception ex)
 90             {
 91                 // TxtLog.WriteTxt(CommonMethod.GetProgramName(), string.Format("SFTP文件上傳失敗,緣由:{0}", ex.Message));
 92                 throw new Exception(string.Format("SFTP文件上傳失敗,緣由:{0}", ex.Message));
 93             }
 94         }
 95         #endregion
 96 
 97         #region SFTP獲取文件
 98         /// <summary>
 99         /// SFTP獲取文件
100         /// </summary>
101         /// <param name="remotePath">遠程路徑</param>
102         /// <param name="localPath">本地路徑</param>
103         public void Get(string remotePath, string localPath)
104         {
105             try
106             {
107                 Connect();
108                 var byt = sftp.ReadAllBytes(remotePath);
109                 Disconnect();
110                 File.WriteAllBytes(localPath, byt);
111             }
112             catch (Exception ex)
113             {
114                 // TxtLog.WriteTxt(CommonMethod.GetProgramName(), string.Format("SFTP文件獲取失敗,緣由:{0}", ex.Message));
115                 throw new Exception(string.Format("SFTP文件獲取失敗,緣由:{0}", ex.Message));
116             }
117 
118         }
119         #endregion
120 
121         #region 刪除SFTP文件
122         /// <summary>
123         /// 刪除SFTP文件 
124         /// </summary>
125         /// <param name="remoteFile">遠程路徑</param>
126         public void Delete(string remoteFile)
127         {
128             try
129             {
130                 Connect();
131                 sftp.Delete(remoteFile);
132                 Disconnect();
133             }
134             catch (Exception ex)
135             {
136                 // TxtLog.WriteTxt(CommonMethod.GetProgramName(), string.Format("SFTP文件刪除失敗,緣由:{0}", ex.Message));
137                 throw new Exception(string.Format("SFTP文件刪除失敗,緣由:{0}", ex.Message));
138             }
139         }
140         #endregion
141 
142         #region 獲取SFTP文件列表
143         /// <summary>
144         /// 獲取SFTP文件列表
145         /// </summary>
146         /// <param name="remotePath">遠程目錄</param>
147         /// <param name="fileSuffix">文件後綴</param>
148         /// <returns></returns>
149         public ArrayList GetFileList(string remotePath, string fileSuffix)
150         {
151             try
152             {
153                 Connect();
154                 var files = sftp.ListDirectory(remotePath);
155                 Disconnect();
156                 var objList = new ArrayList();
157                 foreach (var file in files)
158                 {
159                     string name = file.Name;
160                     if (name.Length > (fileSuffix.Length + 1) && fileSuffix == name.Substring(name.Length - fileSuffix.Length))
161                     {
162                         objList.Add(name);
163                     }
164                 }
165                 return objList;
166             }
167             catch (Exception ex)
168             {
169                 // TxtLog.WriteTxt(CommonMethod.GetProgramName(), string.Format("SFTP文件列表獲取失敗,緣由:{0}", ex.Message));
170                 throw new Exception(string.Format("SFTP文件列表獲取失敗,緣由:{0}", ex.Message));
171             }
172         }
173         #endregion
174 
175         #region 移動SFTP文件
176         /// <summary>
177         /// 移動SFTP文件
178         /// </summary>
179         /// <param name="oldRemotePath">舊遠程路徑</param>
180         /// <param name="newRemotePath">新遠程路徑</param>
181         public void Move(string oldRemotePath, string newRemotePath)
182         {
183             try
184             {
185                 Connect();
186                 sftp.RenameFile(oldRemotePath, newRemotePath);
187                 Disconnect();
188             }
189             catch (Exception ex)
190             {
191                 // TxtLog.WriteTxt(CommonMethod.GetProgramName(), string.Format("SFTP文件移動失敗,緣由:{0}", ex.Message));
192                 throw new Exception(string.Format("SFTP文件移動失敗,緣由:{0}", ex.Message));
193             }
194         }
195         #endregion
196 
197     }
相關文章
相關標籤/搜索