好久不见,我又回来了。给大家分享一个最近c#代码操作ftp服务器的代码示例
  - 1 public abstract class FtpOperation
- 2 {
- 3 /// <summary>
- 4 /// FTP服务器地址
- 5 /// </summary>
- 6 private string ftpServer;
- 7
- 8 /// <summary>
- 9 /// 用户名
- 10 /// </summary>
- 11 private string userName;
- 12
- 13 /// <summary>
- 14 /// 密码
- 15 /// </summary>
- 16 private string passWord;
- 17
- 18 /// <summary>
- 19 /// FTPHelper类的构造函数
- 20 /// </summary>
- 21 /// <param name="ftpServer">FTP服务器地址</param>
- 22 /// <param name="userName">用户名</param>
- 23 /// <param name="passWord">密码</param>
- 24 public FtpOperation(string ftpServer, string userName, string passWord)
- 25 {
- 26 this.ftpServer = ftpServer;
- 27 this.userName = userName;
- 28 this.passWord = passWord;
- 29 }
- 30
- 31 /// <summary>
- 32 /// 执行FTP操作的方法
- 33 /// </summary>
- 34 /// <param name="action">要执行的操作</param>
- 35 private void ExecuteFtpOperation(Action action)
- 36 {
- 37 try
- 38 {
- 39 action.Invoke();
- 40 }
- 41 catch (WebException ex)
- 42 {
- 43 if (ex.Status == WebExceptionStatus.Timeout)
- 44 {
- 45 Console.WriteLine("连接超时。");
- 46 }
- 47 else
- 48 {
- 49 Console.WriteLine("发生错误 WebException: {0}", ex.Message);
- 50 }
- 51 }
- 52 catch (Exception ex)
- 53 {
- 54 Console.WriteLine("发生错误: {0}", ex.Message);
- 55 }
- 56 }
- 57 }
- 58 }
复制代码 基础类的构造函数和属性
FtpOperation 中其他的方法调用示例- // FTP 服务器地址
- string ftpServer = "ftp://127.0.0.1:27/";
- // FTP 服务器用户名
- string userName = "Administrator";
- // FTP 服务器密码
- string password = "admin";
- FtpTest ftp = new FtpTest(ftpServer, userName, password);
- //ftp.QueryAll("/Template"); //查询
- ftp.FtpDeleteFolders("");//删除所有
- ftp.FtpUploadFolder("e:\\CoaTemplate", "");//将文件夹的内容上传到根目录
- ftp.FtpUploadFolder(@"D:\GitCode\Blog.Core", "/gitCode/Blog.Core");//将本地文件夹的内容上传到指定目录
- var data = ftp.RecursiveQueryAll("");//查询所有文件信息
- ftp.FtpMoveFolder("/CoaTemplate", "/1/CoaTemplate");//文件夹移动
- ftp.FtpDownloadFolder("/1", "d:\\1\"); //将ftp服务器的指定文件夹下载到本地目录
复制代码 贴了半天代码,都不太行,一会能展开,一会展不开,源码地址放下面了。
项目地址:https://github.com/yycb1994/FtpSiteManager
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |