c# FTP上传文件。

东西不是很复杂,不过百度出来的,貌似都是一种,代码太长了,复制都不想复制,来个简易版本的吧,直接贴代码。

 1    /// <summary>
 2         /// 上传ftp服务
 3         /// </summary>
 4         /// <param name="path">文件地址</param>
 5         /// <returns></returns>
 6         public string Upload(string path)
 7         {
 8             var client = new WebClient();
 9             client.Credentials = new NetworkCredential("fptuser", "ftppwd");//用户名和密码
10             client.BaseAddress = "ftpurl";//ftp地址
11             string ftpPath = client.BaseAddress + "/TestFileUpLoad/" + Guid.NewGuid();//上传fptp路径
12             string returnPath = "";
13             try
14             {
15                 client.UploadFile(ftpPath, path);
16                 returnPath = ftpPath;
17             }
18             catch (Exception ex)
19             {
20                 ///错误信息处理
21             }
22             return returnPath;
23         }

 

posted @ 2017-12-05 17:58  梦游生  阅读(2806)  评论(0编辑  收藏  举报