Stream ftpRespStream = FTPResp.GetResponseStream();

  StreamReader reader =

   new StreamReader(ftpRespStream, System.Text.Encoding.UTF8);

  //---obtain the result as a string array---

  string[] result = reader.ReadToEnd().Split(

   Environment.NewLine.ToCharArray(),

   StringSplitOptions.RemoveEmptyEntries);

  FTPResp.Close();

  return result;

 } catch (Exception ex) {

  MessageBox.Show(ex.ToString());

  return null;

 }

}

To view the directory listing of an FTP server, you make use of the PerformWebRequest() helper function, which is defined as follows:

private FtpWebResponse PerformWebRequest(

 string path, WebRequestMethod method) {

 //---display the hour glass cursor---

 Cursor.Current = Cursors.WaitCursor;

 FtpWebRequest ftpReq = (FtpWebRequest)WebRequest.Create(path);

 switch (method) {

 case WebRequestMethod.DeleteFile:

  ftpReq.Method = WebRequestMethods.Ftp.DeleteFile;

  break;

 case WebRequestMethod.DownloadFile:

  ftpReq.Method = WebRequestMethods.Ftp.DownloadFile;

  break;

 case WebRequestMethod.ListDirectoryDetails:

  ftpReq.Method = WebRequestMethods.Ftp.ListDirectoryDetails;

  break;

 case WebRequestMethod.MakeDirectory:

  ftpReq.Method = WebRequestMethods.Ftp.MakeDirectory;

  break;

 case WebRequestMethod.RemoveDirectory:

  ftpReq.Method = WebRequestMethods.Ftp.RemoveDirectory;

  break;

 }

 ftpReq.Credentials = new NetworkCredential(

  Properties.Settings.Default.UserName,

  Properties.Settings.Default.Password);

 FtpWebResponse ftpResp = (FtpWebResponse)ftpReq.GetResponse();

 //---change back the cursor---

 Cursor.Current = Cursors.Default;

 return ftpResp;

}

The PerformWebRequest() function contains two parameters:

□ A path representing the full FTP path

□ A WebRequestMethod enumeration representing the type of request you are performing

In the PerformWebRequest() function, you perform the following:

□ Create an instance of the FtpWebRequest class, using the WebRequest class's Create() method. Create() takes in a URI parameter (containing the full FTP path).

□ Set the command to be sent to the FTP server, using the Method property of the FtpWebRequest object.

□ Specify the login credential to the FTP server, using the NetWorkCredential class.

□ Obtain the response from the FTP server, using the GetResponse() method from the FtpWebRequest class.

The PerformWebRequest() function returns a FtpWebResponse object.

Back in the GetDirectoryListing() function, after the call to PerformWebRequest() returns, you retrieve the stream containing the response data sent by the FTP server, using the GetResponseStream() method from the FtpWebResponse class. You then use a StreamReader object to read the directory listing:

//---Get the file/dir listings and return them as a string array---

private string[] GetDirectoryListing(string path) {

 try {

  //---get the directory listing---

  FtpWebResponse FTPResp = PerformWebRequest(

   path, WebRequestMethod.ListDirectoryDetails);

  //---get the stream containing the directory listing---

  Stream ftpRespStream = FTPResp.GetResponseStream ();

  StreamReader reader =

   new StreamReader(ftpRespStream, System.Text.Encoding.UTF8);

  //---obtain the result as a string array---

  string[] result = reader.ReadToEnd().Split(

   Environment.NewLine.ToCharArray(),

   StringSplitOptions.RemoveEmptyEntries);

  FTPResp.Close();

  return result;

 } catch (Exception ex) {

  MessageBox.Show(ex.ToString());

  return null;

 }

}

The directory listing is split into a string array. The directory listings are separated by newline characters. If your FTP server is configured with an MS-DOS directory listing style (see Figure 16-11), the directory listing will look something like this:

12-11-06 10:54PM       2074750 DSC00098.JPG

12-11-06 10:54PM       2109227 DSC00099.JPG

12-11-06 10:49PM <DIR>         George

12-11-06 10:49PM <DIR>         James

12-11-06 10:58PM <DIR>         Wei-Meng Lee

Figure 16-11

Вы читаете C# 2008 Programmer's Reference
Добавить отзыв
ВСЕ ОТЗЫВЫ О КНИГЕ В ИЗБРАННОЕ

0

Вы можете отметить интересные вам фрагменты текста, которые будут доступны по уникальной ссылке в адресной строке браузера.

Отметить Добавить цитату