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;
}
}
}