1 Ftp 上传
  2 
  3 //上传FTP 
  4         public static string UpFtpServer="192.168.100.111";
  5         public static string UpUserID= "sa";
  6         public static string UpPassWord = "123456";
  7 
  8  private void btnUp_Click(object sender, EventArgs e)
  9         {
 10             try
 11             {
 12                upFile();
 13             }
 14             catch (Exception ex)
 15             {
 16                 MessageBoxEx.Show("上传文件发生错误,错误信息:" + ex.Message, "注意", MessageBoxButtons.OK, MessageBoxIcon.Information);
 17             }
 18         }
 19 
 20  //上传文件到ftp
 21         private void upFile()
 22         {
 23 
 24             #region 上传的文件路径
 25             string directoryPath = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
 26             int nIndex = directoryPath.LastIndexOf("\\");
 27             directoryPath = directoryPath.Substring(0, nIndex);
 28             directoryPath += "\\InData\\";
 29             #endregion
 30             string[] fileName = Directory.GetFiles(directoryPath, "*.txt");
 31 
 32             Array array = Array.CreateInstance(typeof(string), fileName.Length);
 33 
 34             fileName.CopyTo(array, 0);
 35             IList<string> successname = new List<string>();
 36             IList<string> failname = new List<string>();
 37 
 38             string address = "", userName = "", pass = "", Report = "";
 39 
 40             for (int i = 0; i < array.Length; i++)
 41             {
 42                 if (File.Exists(array.GetValue(i).ToString()))
 43                 {
 44                     FileInfo fi = new FileInfo(array.GetValue(i).ToString());
 45                     FileStream fs = fi.OpenRead();                    //以只读的模式创建一个文件流
 46                     Stream s = null;
 47                     try
 48                     {
 49                         address = UpFtpServer.Trim();
 50                         address = address.ToUpper().IndexOf("FTP://") < 0 ? ("FTP://" + address) : address;
 51                         userName = UpUserID.Trim();
 52                         pass = UpPassWord.Trim();
 53 
 54                         FtpWebRequest fwr = (FtpWebRequest)FtpWebRequest.Create(address + "/InData/" + Path.GetFileName(array.GetValue(i).ToString()));//创建一个ftpwebrequest对象                             
 55 
 56                         fwr.Credentials = new NetworkCredential(userName, pass);
 57 
 58                         fwr.KeepAlive = true;                             //执行完一次命令后是否保持连接                             
 59                         fwr.Method = WebRequestMethods.Ftp.UploadFile;    //指出要执行的命令方式                            
 60                         fwr.UseBinary = true;                             //指出文件的数据传输类型                             
 61                         fwr.UsePassive = true;
 62 
 63                         int length = unchecked(Convert.ToInt32(fi.Length));
 64                         fwr.ContentLength = length;
 65                         s = fwr.GetRequestStream();
 66 
 67                         if (length <= 1000 * 1024)    //文件上传的大小不能大于1000KB足够啦
 68                         {
 69                             byte[] b = new byte[length];
 70                             //先利用一个fileinfo跟一个文本文件关联起来 然后利用fileinfo的openread以只读的方式打开文本文件
 71                             //并返回一个filestream文件流
 72                             fs.Read(b, 0, length);
 73                             s.Write(b, 0, length);
 74 
 75                             if (successname.IndexOf(array.GetValue(i).ToString()) < 0)
 76                             {
 77                                 successname.Add(array.GetValue(i).ToString());
 78                             }
 79 
 80                             Report = Report + "," + Path.GetFileName(array.GetValue(i).ToString());
 81                         }
 82 
 83                         fs.Close();
 84                         s.Close();
 85                     }
 86                     catch (Exception)
 87                     {
 88                         if (failname.IndexOf(array.GetValue(i).ToString()) < 0)
 89                         {
 90                             failname.Add(array.GetValue(i).ToString());
 91                         }
 92                     }
 93                     finally
 94                     {
 95                         fs.Close();
 96                     }
 97                 }
 98 
 99             }
100 
101             foreach (string filename in successname)
102             {
103                 if (failname.IndexOf(filename) < 0)
104                 {
105                     File.Delete(filename);          //上传完毕后就删除
106                 }
107             }
108             MessageBoxEx.Show("本次共成功上传了" + successname.Count.ToString() + "个文件,失败" + failname.Count.ToString() + "个!", "注意", MessageBoxButtons.OK, MessageBoxIcon.Information);
109         }
上传

相关文章: