winform通过FTP协议上传下载文件

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了winform通过FTP协议上传下载文件相关的知识,希望对你有一定的参考价值。

上传文件:窗体代码

一次上传多个文件(grdAffixFilesList中需要上传的)

private Boolean UploadFile()
        {
            string filename;
            int upCount=0;
            for (int i = 0; i < this.grdAffixFilesList.Rows.Count; i++)
            {
                filename = this.grdAffixFilesList.Rows[i].Cells["FILEPATH"].Text.ToString();
                if (fm.UpLoadFile(filename, getFileCatalog()))
                    upCount++;
            }
            if (upCount == this.grdAffixFilesList.Rows.Count)
                return true;
            else
            {
                MessageBox.Show("附件上传失败,请检查", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }
        }

窗体代码:生产上传目录

技术分享
 1 private string getFileCatalog()
 2         {
 3             string ftpfilepath = "FTPUpload/";
 4            // string dt = DateTime.Now.ToString("yyyy-MM-dd");
 5             string year = DateTime.Now.Year.ToString();
 6             string month = DateTime.Now.Month.ToString();
 7             string day = DateTime.Now.Day.ToString();
 8             string exceptionType =this.cboFileExceptionType.Text;
 9             ftpfilepath += year+"/"+month+"/"+day+"/"+exceptionType;
10             return ftpfilepath;
11         }
View Code

要调用的上传文件的方法

技术分享
 1 public Boolean UpLoadFile(string fn,string ftpfilepath)
 2         {
 3             return Upload(fn, ftpfilepath);
 4         }
 5 private Boolean Upload(string filename,string ftpfilepath)
 6         {
 7            
 8             FileInfo fileInf = new FileInfo(filename);
 9             string uri = "ftp://" + ftpServerIP + "/"+ftpfilepath+"/" + fileInf.Name;
10             FtpWebRequest reqFTP;
11             FtpCheckDirectoryExist(ftpfilepath);
12             // Create FtpWebRequest object from the Uri provided
13             reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
14 
15             // Provide the WebPermission Credintials
16             reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
17 
18             // By default KeepAlive is true, where the control connection is not closed
19             // after a command is executed.
20             reqFTP.KeepAlive = false;
21 
22             // Specify the command to be executed.
23             reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
24 
25             // Specify the data transfer type.
26             reqFTP.UseBinary = true;
27 
28             // Notify the server about the size of the uploaded file
29             reqFTP.ContentLength = fileInf.Length;
30 
31             // The buffer size is set to 2kb
32             int buffLength = 2048;
33             byte[] buff = new byte[buffLength];
34             int contentLen;
35 
36             // Opens a file stream (System.IO.FileStream) to read the file to be uploaded
37             FileStream fs = fileInf.OpenRead();
38 
39             try
40             {
41                 // Stream to which the file to be upload is written
42                 Stream strm = reqFTP.GetRequestStream();
43 
44                 // Read from the file stream 2kb at a time
45                 contentLen = fs.Read(buff, 0, buffLength);
46 
47                 // Till Stream content ends
48                 while (contentLen != 0)
49                 {
50                     // Write Content from the file stream to the FTP Upload Stream
51                     strm.Write(buff, 0, contentLen);
52                     contentLen = fs.Read(buff, 0, buffLength);
53                 }
54 
55                 // Close the file stream and the Request Stream
56                 strm.Close();
57                 fs.Close();
58             }
59             catch (Exception ex)
60             {
61                 MessageBox.Show(ex.Message, "Upload Error");
62                 return false;
63             }
64             finally
65             {
66 
67                 fs.Close();
68                 reqFTP.Abort();
69 
70             }
71             return true;
72         }
View Code

窗体代码:下载文件

技术分享
 1 private void DownloadFile()
 2         {
 3             string savepath = null;
 4             string ftpfilepath = "/FTPTest/2017-04-26/";//要下载的文件的FTP路径
 5             FolderBrowserDialog savePath = new FolderBrowserDialog();//通过FolderBrowserDialog控件获取文件保存地址
 6             if (savePath.ShowDialog() == DialogResult.OK)
 7             {
 8                 savepath = savePath.SelectedPath;
 9             }
10             if (fm.DownLoadFile(savepath, "ftptest.txt", ftpfilepath))
11                 MessageBox.Show("文件下载成功");
12         }
View Code

方法代码:下载文件

技术分享
public Boolean DownLoadFile(string savepath,string fn,string ftpfilepath)
        {
            // Local PC File download path
            return Download(savepath, fn, ftpfilepath);
        }

private Boolean Download(string filePath, string fileName,string ftpfilepath)
        {
            FtpWebRequest reqFTP;
            try
            {
                //filePath = <<The full path where the file is to be created.>>, 
                //fileName = <<Name of the file to be created(Need not be the name of the file on FTP server).>>
                FileStream outputStream = new FileStream(filePath + "\\" + fileName, FileMode.Create);

                reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + ftpfilepath + fileName));
                reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
                reqFTP.UseBinary = true;
                reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
                FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
                Stream ftpStream = response.GetResponseStream();
                //int cl = Convert.ToInt32(response.ContentLength);
                int cl = 2048;
                int bufferSize = cl;
                int readCount;
                byte[] buffer = new byte[bufferSize];

                readCount = ftpStream.Read(buffer, 0, bufferSize);
                while (readCount > 0)
                {
                    outputStream.Write(buffer, 0, readCount);
                    readCount = ftpStream.Read(buffer, 0, bufferSize);
                }

                ftpStream.Close();
                outputStream.Close();
                response.Close();
                return true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return false;
            }
        }
View Code

附:FTP上传下载文件的其他方法

技术分享
  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Text;
  5 using System.Net;
  6 using System.IO;
  7 using System.Windows.Forms;
  8 
  9 namespace MES.Common.Entities
 10 {
 11     [Serializable]
 12     public class FTPMETHOD
 13     {
 14         private string ftpServerIP, ftpUserID, ftpPassword;
 15 
 16         //private string ftpServerFilePath = "/temp/ftptest/";
 17         private string ftpServerFilePath = "/Abnormal/2017-04-07/";
 18         //private string ftpServerFilePath = "/Test";
 19 
 20         private string filePath = "";
 21 
 22         public string ftppassword;
 23         public string FTPPASSWORD
 24         {
 25             get { return ftppassword; }
 26             set { ftppassword = value; }
 27         }
 28 
 29         public string ftpuserid;
 30         public string FTPUSERID
 31         {
 32             get { return ftpuserid; }
 33             set { ftpuserid = value; }
 34         }
 35 
 36         public string ftpserverip;
 37         public string FTPSERVERIP
 38         {
 39             get { return ftpserverip; }
 40             set { ftpserverip = value; }
 41         }
 42 
 43         //2014.12.10 lwxu
 44         public string ftpserverfilepath1;
 45         public string FTPSERVERFILEPATH1
 46         {
 47             get { return ftpserverfilepath1; }
 48             set { ftpserverfilepath1 = value; }
 49         }
 50 
 51         public FTPMETHOD() : this(string.Empty, string.Empty, string.Empty) { }
 52 
 53         public FTPMETHOD(string ServerIP, string UserID, string Password)
 54         {
 55             this.ftpServerIP = ServerIP;
 56             this.ftpUserID = UserID;
 57             this.ftpPassword = Password;
 58         }
 59 
 60         /// <summary>
 61         /// Method to upload the specified file to the specified FTP Server
 62         /// </summary>
 63         /// <param name="filename">file full name to be uploaded</param>
 64         /// 
 65         private Boolean Upload(string filename,string ftpfilepath)
 66         {
 67            
 68             FileInfo fileInf = new FileInfo(filename);
 69             string uri = "ftp://" + ftpServerIP + "/"+ftpfilepath+"/" + fileInf.Name;
 70             FtpWebRequest reqFTP;
 71             FtpCheckDirectoryExist(ftpfilepath);
 72             // Create FtpWebRequest object from the Uri provided
 73             reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
 74 
 75             // Provide the WebPermission Credintials
 76             reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
 77 
 78             // By default KeepAlive is true, where the control connection is not closed
 79             // after a command is executed.
 80             reqFTP.KeepAlive = false;
 81 
 82             // Specify the command to be executed.
 83             reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
 84 
 85             // Specify the data transfer type.
 86             reqFTP.UseBinary = true;
 87 
 88             // Notify the server about the size of the uploaded file
 89             reqFTP.ContentLength = fileInf.Length;
 90 
 91             // The buffer size is set to 2kb
 92             int buffLength = 2048;
 93             byte[] buff = new byte[buffLength];
 94             int contentLen;
 95 
 96             // Opens a file stream (System.IO.FileStream) to read the file to be uploaded
 97             FileStream fs = fileInf.OpenRead();
 98 
 99             try
100             {
101                 // Stream to which the file to be upload is written
102                 Stream strm = reqFTP.GetRequestStream();
103 
104                 // Read from the file stream 2kb at a time
105                 contentLen = fs.Read(buff, 0, buffLength);
106 
107                 // Till Stream content ends
108                 while (contentLen != 0)
109                 {
110                     // Write Content from the file stream to the FTP Upload Stream
111                     strm.Write(buff, 0, contentLen);
112                     contentLen = fs.Read(buff, 0, buffLength);
113                 }
114 
115                 // Close the file stream and the Request Stream
116                 strm.Close();
117                 fs.Close();
118             }
119             catch (Exception ex)
120             {
121                 MessageBox.Show(ex.Message, "Upload Error");
122                 return false;
123             }
124             finally
125             {
126 
127                 fs.Close();
128                 reqFTP.Abort();
129 
130             }
131             return true;
132         }
133 
134         public void FtpCheckDirectoryExist(string destFilePath)
135         {
136 
137             //string fullDir = FtpParseDirectory(destFilePath);
138 
139             string[] dirs = destFilePath.Split(/);
140 
141             string curDir = "";
142             string subcatalog = "";
143             for (  int i = 0; i < dirs.Length; i++)
144             {
145 
146                 if (i > 0 && i <= dirs.Length)
147                     subcatalog = subcatalog + "/" + dirs[i - 1];
148 
149                 if (dirs[i] != null && dirs[i].Length > 0)
150                 {
151 
152                     try
153                     {
154                         curDir += "/"+ dirs[i] ;
155                         //curDir = curDir.Remove(0,1);
156                         string[] exitcatalog = GetFileList1(subcatalog);
157                         if (exitcatalog == null)
158                         {
159                             
160                             FtpMakeDir(curDir);
161                         }
162                         else
163                         {
164                             for (int j = 0; j < exitcatalog.Length; j++)
165                             {
166                                 exitcatalog[j] = "/" + exitcatalog[j];
167                                 if (curDir == exitcatalog[j])
168                                 {
169                                     break;
170                                 }
171                                 if (j == exitcatalog.Length - 1)
172                                 {
173                                     //curDir += dirs[i] + "/";
174                                     FtpMakeDir(curDir);
175                                 }
176                             }
177                         }
178                             
179 
180                     }
181 
182                     catch (Exception)
183 
184                     { }
185 
186                 }
187 
188             }
189 
190         }
191 
192 
193 
194         public static string FtpParseDirectory(string destFilePath)
195         {
196 
197             return destFilePath.Substring(0, destFilePath.LastIndexOf("/"));
198 
199         }
200 
201 
202 
203         //创建目录
204 
205         private  Boolean FtpMakeDir(string localFile)
206         {
207 
208             FtpWebRequest req = (FtpWebRequest)WebRequest.Create("ftp://"+ftpServerIP + localFile);
209 
210             req.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
211 
212             req.Method = WebRequestMethods.Ftp.MakeDirectory;
213 
214             try
215             {
216 
217                 FtpWebResponse response = (FtpWebResponse)req.GetResponse();
218 
219                 response.Close();
220 
221             }
222 
223             catch (Exception)
224             {
225 
226                 req.Abort();
227 
228                 return false;
229 
230             }
231 
232             req.Abort();
233 
234             return true;
235 
236         }
237 
238 
239 
240 
241         private void DeleteFTP(string fileName, string pathFlag)
242         {
243             try
244             {
245                 string uri = "ftp://" + ftpServerIP + "/" + fileName;
246                 FtpWebRequest reqFTP;
247                 reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + ftpServerFilePath + pathFlag + fileName));
248 
249                 reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
250                 reqFTP.KeepAlive = false;
251                 reqFTP.Method = WebRequestMethods.Ftp.DeleteFile;
252 
253                 string result = String.Empty;
254                 FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
255                 long size = response.ContentLength;
256                 Stream datastream = response.GetResponseStream();
257                 StreamReader sr = new StreamReader(datastream);
258                 result = sr.ReadToEnd();
259                 sr.Close();
260                 datastream.Close();
261                 response.Close();
262             }
263             catch (Exception ex)
264             {
265                 MessageBox.Show(ex.Message, "FTP 2.0 Delete");
266             }
267         }
268 
269         //lwxu
270 
271         private void DeleteFTP1(string fileName, string ftpServerFilePath)
272         {
273             try
274             {
275                 string uri = "ftp://" + ftpServerIP + "/" + fileName;
276                 FtpWebRequest reqFTP;
277                 reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + ftpserverfilepath1 + "/" + fileName));
278 
279                 reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
280                 reqFTP.KeepAlive = false;
281                 reqFTP.Method = WebRequestMethods.Ftp.DeleteFile;
282 
283                 string result = String.Empty;
284                 FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
285                 long size = response.ContentLength;
286                 Stream datastream = response.GetResponseStream();
287                 StreamReader sr = new StreamReader(datastream);
288                 result = sr.ReadToEnd();
289                 sr.Close();
290                 datastream.Close();
291                 response.Close();
292             }
293             catch (Exception ex)
294             {
295                 MessageBox.Show(ex.Message, "FTP 2.0 Delete");
296             }
297         }
298         
299         private string[] GetFilesDetailList()
300         {
301             string[] downloadFiles;
302             try
303             {
304                 StringBuilder result = new StringBuilder();
305                 FtpWebRequest ftp;
306                 ftp = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + ftpServerFilePath));
307                 ftp.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
308                 ftp.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
309                 WebResponse response = ftp.GetResponse();
310                 StreamReader reader = new StreamReader(response.GetResponseStream());
311                 string line = reader.ReadLine();
312                 while (line != null)
313                 {
314                     result.Append(line);
315                     result.Append("\n");
316                     line = reader.ReadLine();
317                 }
318 
319                 result.Remove(result.ToString().LastIndexOf("\n"), 1);
320                 reader.Close();
321                 response.Close();
322                 return result.ToString().Split(\n);
323                 //MessageBox.Show(result.ToString().Split(‘\n‘));
324             }
325             catch (Exception ex)
326             {
327                 System.Windows.Forms.MessageBox.Show(ex.Message);
328                 downloadFiles = null;
329                 return downloadFiles;
330             }
331         }
332 
333         private string[] GetFileList(string pathFlag)
334         {
335             string[] downloadFiles;
336             StringBuilder result = new StringBuilder();
337             FtpWebRequest reqFTP;
338             try
339             {
340                 reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + ftpServerFilePath + pathFlag));
341                 reqFTP.UseBinary = true;
342                 reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
343                 reqFTP.Method = WebRequestMethods.Ftp.ListDirectory;
344                 WebResponse response = reqFTP.GetResponse();
345                 StreamReader reader = new StreamReader(response.GetResponseStream());
346                 //MessageBox.Show(reader.ReadToEnd());
347                 string line = reader.ReadLine();
348                 while (line != null)
349                 {
350                     result.Append(line);
351                     result.Append("\n");
352                     line = reader.ReadLine();
353                 }
354                 result.Remove(result.ToString().LastIndexOf(\n), 1);
355                 reader.Close();
356                 response.Close();
357                 //MessageBox.Show(response.StatusDescription);
358                 return result.ToString().Split(\n);
359             }
360             catch (Exception ex)
361             {
362                 System.Windows.Forms.MessageBox.Show(ex.Message);
363                 downloadFiles = null;
364                 return downloadFiles;
365             }
366         }
367 
368         //lwxu 
369         private string[] GetFileList1(string subcatalog)
370         {
371             string[] downloadFiles;
372             StringBuilder result = new StringBuilder();
373             FtpWebRequest reqFTP;
374             try
375             {
376                 reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP+subcatalog));
377                 reqFTP.UseBinary = true;
378                 reqFTP.KeepAlive = false;
379                 reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
380                 reqFTP.Method = WebRequestMethods.Ftp.ListDirectory;
381                 WebResponse response = reqFTP.GetResponse();
382                 StreamReader reader = new StreamReader(response.GetResponseStream());
383                 //MessageBox.Show(reader.ReadToEnd());
384                 string line = reader.ReadLine();
385                 while (line != null)
386                 {
387                     result.Append(line).Replace("Test/","");
388                     result.Append("\n");
389                     line = reader.ReadLine();
390                 }
391                 result.Remove(result.ToString().LastIndexOf(\n), 1);
392                 reader.Close();
393                 response.Close();
394                 //MessageBox.Show(response.StatusDescription);
395                 return result.ToString().Split(\n);
396             }
397             catch (Exception ex)
398             {
399                // System.Windows.Forms.MessageBox.Show(ex.Message);
400                 downloadFiles = null;
401                 return downloadFiles;
402             }
403         }
404 
405         //lwxu
406         public void DownLoadFile1(string fn, string ftpServerFilePath)
407         {
408             // Local PC File download path
409             Download1(Application.StartupPath, fn, ftpServerFilePath);
410         }
411 
412         //lwxu 
413         private void Download1(string filePath, string fileName,string ftpServerFilePath)
414         {
415             FtpWebRequest reqFTP;
416             try
417             {
418                 //filePath = <<The full path where the file is to be created.>>, 
419                 //fileName = <<Name of the file to be created(Need not be the name of the file on FTP server).>>
420                 FileStream outputStream = new FileStream(filePath + "\\" + fileName, FileMode.Create); //查找下载到本地的文件
421 
422                 reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + ftpserverfilepath1 + "/" + fileName));
423                 reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
424                 reqFTP.UseBinary = true;
425                 reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
426                 FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
427                 Stream ftpStream = response.GetResponseStream();
428                 //int cl = Convert.ToInt32(response.ContentLength);
429                 int cl = 2048;
430                 int bufferSize = cl;
431                 int readCount;
432                 byte[] buffer = new byte[bufferSize];
433 
434                 readCount = ftpStream.Read(buffer, 0, bufferSize);
435                 while (readCount > 0)
436                 {
437                     outputStream.Write(buffer, 0, readCount);
438                     readCount = ftpStream.Read(buffer, 0, bufferSize);
439                 }
440 
441                 ftpStream.Close();
442                 outputStream.Close();
443                 response.Close();
444             }
445             catch (Exception ex)
446             {
447                 MessageBox.Show(ex.Message);
448             }
449         }
450 
451         private Boolean Download(string filePath, string fileName,string ftpfilepath)
452         {
453             FtpWebRequest reqFTP;
454             try
455             {
456                 //filePath = <<The full path where the file is to be created.>>, 
457                 //fileName = <<Name of the file to be created(Need not be the name of the file on FTP server).>>
458                 FileStream outputStream = new FileStream(filePath + "\\" + fileName, FileMode.Create);
459 
460                 reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + ftpfilepath + fileName));
461                 reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
462                 reqFTP.UseBinary = true;
463                 reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
464                 FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
465                 Stream ftpStream = response.GetResponseStream();
466                 //int cl = Convert.ToInt32(response.ContentLength);
467                 int cl = 2048;
468                 int bufferSize = cl;
469                 int readCount;
470                 byte[] buffer = new byte[bufferSize];
471 
472                 readCount = ftpStream.Read(buffer, 0, bufferSize);
473                 while (readCount > 0)
474                 {
475                     outputStream.Write(buffer, 0, readCount);
476                     readCount = ftpStream.Read(buffer, 0, bufferSize);
477                 }
478 
479                 ftpStream.Close();
480                 outputStream.Close();
481                 response.Close();
482                 return true;
483             }
484             catch (Exception ex)
485             {
486                 MessageBox.Show(ex.Message);
487                 return false;
488             }
489         }
490 
491         private long GetFileSize(string filename, string machineName)
492         {
493             FtpWebRequest reqFTP;
494             long fileSize = 0;
495             try
496             {
497 
498                 
499 
500                 reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + filePath + filename));
501                 reqFTP.Method = WebRequestMethods.Ftp.GetFileSize;
502                 reqFTP.UseBinary = true;
503                 reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
504                 FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
505                 Stream ftpStream = response.GetResponseStream();
506                 fileSize = response.ContentLength;
507 
508                 ftpStream.Close();
509                 response.Close();
510             }
511             catch (Exception ex)
512             {
513                 MessageBox.Show(ex.Message);
514             }
515             return fileSize;
516         }
517 
518         private void Rename(string currentFilename, string newFilename)
519         {
520             FtpWebRequest reqFTP;
521             try
522             {
523                 reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + ftpServerFilePath + currentFilename));
524                 reqFTP.Method = WebRequestMethods.Ftp.Rename;
525                 reqFTP.RenameTo = newFilename;
526                 reqFTP.UseBinary = true;
527                 reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
528                 FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
529                 Stream ftpStream = response.GetResponseStream();
530 
531                 ftpStream.Close();
532                 response.Close();
533             }
534             catch (Exception ex)
535             {
536                 MessageBox.Show(ex.Message);
537             }
538         }
539 
540         private void MakeDir(string dirName)
541         {
542             FtpWebRequest reqFTP;
543             try
544             {
545                 // dirName = name of the directory to create.
546                 reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + ftpServerFilePath + dirName));
547                 reqFTP.Method = WebRequestMethods.Ftp.MakeDirectory;
548                 reqFTP.UseBinary = true;
549                 reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
550                 FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
551                 Stream ftpStream = response.GetResponseStream();
552 
553                 ftpStream.Close();
554                 response.Close();
555             }
556             catch (Exception ex)
557             {
558                 MessageBox.Show(ex.Message);
559             }
560         }
561 
562         public Boolean UpLoadFile(string fn,string ftpfilepath)
563         {
564             return Upload(fn, ftpfilepath);
565         }
566 
567         public Boolean DownLoadFile(string savepath,string fn,string ftpfilepath)
568         {
569             // Local PC File download path
570             return Download(savepath, fn, ftpfilepath);
571         }
572 
573         public string[] FileList(string pathFlag)
574         {
575             return GetFileList(pathFlag);
576         }
577 
578         //lwxu 2014.12.10 读取文件名
579         //public string[] FileList1(string subcatalog)
580         //{
581         //   // return GetFileList1(subcatalog);
582         //}
583 
584         public void DeleteFile(string fn, string pathFlag)
585         {
586             DeleteFTP(fn, pathFlag);
587         }
588 
589         //lwxu
590         public void DeleteFile1(string fn,string ftpServerFilePath)
591         {
592             DeleteFTP1(fn, ftpServerFilePath);
593         }
594 
595         //public string[] FilesDetailList()
596         //{
597         //    //return GetFilesDetailList();
598         //    //return GetFileList1();
599         //}
600 
601     }
602 }
View Code

 

以上是关于winform通过FTP协议上传下载文件的主要内容,如果未能解决你的问题,请参考以下文章

C# winform如何实现批量上传文件到远程服务器?

C# winfrom将文件通过IIS(http协议)上传到服务器上

Ftp文件上传

Ftp文件上传

pythonftp给文件授权

FTP 文件传输协议