Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 3rd, 2012  |  syntax: None  |  size: 3.61 KB  |  hits: 18  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Download Files with FTP using conditional stament
  2. class Program
  3. {
  4.     static void Main(string[] args)
  5.     {
  6.         ConsoleApplication1.Program test = new Program();
  7.         string Name = "SE_TEST";
  8.         string[] filteredfiles = test.GetFileList();
  9.         string[] files = filteredfiles;
  10.     foreach (string file in files)
  11.     {
  12.  
  13.         bool b;
  14.         if (b = file.Contains(Name))
  15.         {
  16.             test.Download(file);
  17.  
  18.         }
  19.     }
  20.     }
  21.     public string[] GetFileList()
  22.     {
  23.         string[] downloadFiles;
  24.         StringBuilder result = new StringBuilder();
  25.         WebResponse response = null;
  26.         StreamReader reader = null;
  27.         try
  28.         {
  29.             FtpWebRequest reqFTP;
  30.             reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + "1.0.0.1" + "/"));
  31.             reqFTP.UseBinary = true;
  32.             reqFTP.Credentials = new NetworkCredential("sh", "SE");
  33.             reqFTP.Method = WebRequestMethods.Ftp.ListDirectory;
  34.             reqFTP.Proxy = null;
  35.             reqFTP.KeepAlive = false;
  36.             reqFTP.UsePassive = false;
  37.             response = reqFTP.GetResponse();
  38.             reader = new StreamReader(response.GetResponseStream());
  39.             string line = reader.ReadLine();
  40.             while (line != null)
  41.             {
  42.                 result.Append(line);
  43.                 result.Append("n");
  44.                 line = reader.ReadLine();
  45.             }
  46.             // to remove the trailing 'n'
  47.             result.Remove(result.ToString().LastIndexOf('n'), 1);
  48.             return result.ToString().Split('n');
  49.         }
  50.         catch (Exception ex)
  51.         {
  52.             if (reader != null)
  53.             {
  54.                 reader.Close();
  55.             }
  56.             if (response != null)
  57.             {
  58.                 response.Close();
  59.             }                
  60.             downloadFiles = null;
  61.             return downloadFiles;
  62.         }
  63.     }
  64.  
  65.     private void Download(string file)
  66.     {                      
  67.  
  68.         try
  69.         {
  70.             FtpWebRequest reqFTP;
  71.             string ftpserverIp = "1.0.0.1";
  72.  
  73.             string fileName = @"c:downloadDir" + file;
  74.             FileInfo downloadFile = new FileInfo(fileName);
  75.             // string uri = "ftp://1.0.0.1";
  76.              FileStream outputStream = new FileStream(fileName, FileMode.Append);
  77.              reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpserverIp + "/" + file));
  78.             reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
  79.             reqFTP.UseBinary = true;
  80.             reqFTP.KeepAlive = false;
  81.             reqFTP.Timeout = -1;
  82.             reqFTP.UsePassive = true;
  83.             reqFTP.Credentials = new NetworkCredential("sh", "SE");
  84.             FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
  85.             Stream ftpStream = response.GetResponseStream();
  86.             long cl = response.ContentLength;
  87.            // reqFTP.Method = WebRequestMethods.Ftp.ListDirectory;
  88.             int bufferSize = 4096;
  89.             int readCount;
  90.             byte[] buffer = new byte[bufferSize];
  91.             readCount = ftpStream.Read(buffer, 0, bufferSize);
  92.             Console.WriteLine("Connected: Downloading File");
  93.             while (readCount > 0)
  94.             {
  95.                 outputStream.Write(buffer, 0, readCount);
  96.                 readCount = ftpStream.Read(buffer, 0, bufferSize);
  97.                 Console.WriteLine(readCount.ToString());
  98.             }
  99.  
  100.             ftpStream.Close();
  101.             outputStream.Close();
  102.             response.Close();
  103.             Console.WriteLine("Downloading Complete");
  104.         }
  105.         catch (Exception ex)
  106.         {
  107.             Console.Write(ex);
  108.         }
  109.  
  110.     }
  111.     }
  112. }