Opening FTP Read Streams

thines01 0 Tallied Votes 401 Views Share

Here are two examples of opening read streams to files on an FTP server.
One example uses the FtpWebRequest method.
One example uses the WebClient method.

Here is a method of testing those functions:

using System;
using System.Diagnostics;
using System.IO;
using System.Net;

namespace UseFtpStreamExample
{
   using FtpReadStreams;
   class Program
   {
      static void Main(string[] args)
      {
         string strError = "";

         string strFtpUri =
#if !DEBUG
            "ftp://ftp.myServer.com/SomeFile.txt";
#else
            "ftp://download.textpad.com/welcome.msg";
#endif

         // Be sure to open an debug output window to see file contents
         if (!CFtpReadStream.DoWebRequestExample(strFtpUri, ref strError))
         {
            Trace.WriteLine("Could not read WR: " + strError);
            return;
         }

         // Be sure to open an debug output window to see file contents
         if (!CFtpReadStream.DoWebClientExample(strFtpUri, ref strError))
         {
            Trace.WriteLine("Could not read WC: " + strError);
            return;
         }

         Trace.WriteLine("Finished");
      }
   }
}
using System;
using System.Diagnostics;
using System.IO;
using System.Net;

namespace FtpReadStreams
{
   public class CFtpReadStream
   {
      /// <summary>
      /// Open a file for reading (as text) and display it in debug window.
      /// This example uses the WebClient
      /// </summary>
      /// <param name="strFtpUri">The full URI to the file</param>
      /// <param name="strError">ref to any errors returned</param>
      /// <returns>bool (true if success)</returns>
      public static bool DoWebClientExample(string strFtpUri, ref string strError)
      {
         bool blnRetVal = true;
         WebClient wc = new WebClient();
         try
         {
            using (StreamReader fileIn = new StreamReader(wc.OpenRead(strFtpUri)))
            {
               string strData = "";
               while (!fileIn.EndOfStream)
               {
                  strData = fileIn.ReadLine();
                  Trace.WriteLine(strData);
               }

               fileIn.Close();
            }
         }
         catch (Exception exc)
         {
            blnRetVal = false;
            strError = exc.Message;
         }

         return blnRetVal;
      }

      /// <summary>
      /// Open a file for reading (as text) and display it in debug window.
      /// This method uses the FtpWebRequest
      /// </summary>
      /// <param name="strFtpUri">The full URI to the file</param>
      /// <param name="strError">ref to any errors returned</param>
      /// <returns>bool (true if success)</returns>
      public static bool DoWebRequestExample(string strFtpUri, ref string strError)
      {
         bool blnRetVal = true;
         try
         {
            FtpWebRequest ftp = (FtpWebRequest)WebRequest.Create(strFtpUri);
            ftp.Method = WebRequestMethods.Ftp.DownloadFile;
            using (FtpWebResponse response = (FtpWebResponse)ftp.GetResponse())
            {
               using (StreamReader fileIn = new StreamReader(response.GetResponseStream()))
               {
                  string strData = "";
                  while (!fileIn.EndOfStream)
                  {
                     strData = fileIn.ReadLine();
                     Trace.WriteLine(strData);
                  }
                  //
                  fileIn.Close();
               }
               //
               response.Close();
            }
         }
         catch (Exception exc)
         {
            blnRetVal = false;
            strError = exc.Message;
         }

         return blnRetVal;
      }
   }
}