FTP 用自帶工具下載和刪除文件

 fileNames = GetFileNames(ftpURI, ftpUserID, ftpPassword);
             


            for (int i = 0; i < fileNames.Count;i++ )
            {


            
                DownloadFtp(fileNames[i].ToString().Trim());








                string path = @"D:\test\" + fileNames[i].ToString().Trim();




                if (File.Exists(path.ToString()))
                {
                    //File.Delete(path);




                    DeleteFtp(fileNames[i].ToString().Trim());
                }




            }


























           public static ArrayList GetFileNames(string ftpDir, string userName, string passWord) 
           {   var fileNames = new ArrayList();    
               var reqFtp = (FtpWebRequest)WebRequest.Create(new Uri(ftpDir));  
               reqFtp.UsePassive = false;       
               reqFtp.UseBinary = true;       
                        reqFtp.Credentials = new NetworkCredential(userName, passWord);        
               reqFtp.Method = WebRequestMethods.Ftp.ListDirectory;      
               var response = (FtpWebResponse)reqFtp.GetResponse();     
               var reader = new StreamReader(response.GetResponseStream(), Encoding.Default);  
               string fileName = reader.ReadLine();      
               while (fileName != null)    
               {   fileNames.Add(Path.GetFileName(fileName));    
                   fileName = reader.ReadLine();   
               }     
               reader.Close();        
               response.Close();       
               //var fileNames = reader.ReadToEnd();   
               reader.Close();        
               response.Close();        
               return fileNames; 
           } 












         public static int DownloadFtp(string filename)
        {
            FtpWebRequest reqFTP;
            string serverIP;
            string userName;
            string password;
            string url;


            try
            {
                serverIP = "ftp://ftp1.qsicn.com//test"; ;
                userName = "user14"; ;
                password = "f17gg59@";


                url = serverIP + "//" + Path.GetFileName(filename);


                string path = @"D:\test\" + filename;
                //FileStream outputStream = new FileStream(filename, FileMode.Create);
                FileStream outputStream = new FileStream(path, FileMode.Create);
                reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(url));
                reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
                reqFTP.UseBinary = true;
                reqFTP.KeepAlive = false;
                reqFTP.Credentials = new NetworkCredential(userName, password);
                FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
                Stream ftpStream = response.GetResponseStream();
                long cl = response.ContentLength;
                int bufferSize = 2048;
                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 0;
            }
            catch (Exception ex)
            {
                SystemLog.logger(ex.InnerException.Message);
                return -2;
            }
        }










         public void DeleteFtp(string fileName)
         {
             try
             {
                 FtpWebRequest reqFTP;
                 reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpURI +"//" +fileName));
                 reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
                 reqFTP.Method = WebRequestMethods.Ftp.DeleteFile;
                 reqFTP.KeepAlive = false;
                 string result = String.Empty;
                 FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
                 long size = response.ContentLength;
                 Stream datastream = response.GetResponseStream();
                 StreamReader sr = new StreamReader(datastream);
                 result = sr.ReadToEnd();
                 sr.Close();
                 datastream.Close();
                 response.Close();
             }
             catch (Exception ex)
             {
                 throw new Exception(ex.Message);
             }
         }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值