FTP connection WebRequestMethods.Ftp.ListDirectory

Last post 10-19-2011 9:58 by acaliaro. 2 replies.
Page 1 of 1 (3 items)
Sort Posts: Previous Next
  • 10-18-2011 17:04

    FTP connection WebRequestMethods.Ftp.ListDirectory

    Hi,

    I was writing to see how to set the request.Method for ftp messages in the OpenNetCF Smart Device.  I can open the ftp port but when I try to set the request.Method =  WebRequestMethods.Ftp.ListDirectory i get an exception 'System.ArgumentException', "Value does not fall within the expected range."

    I would like to get the list of files in a directory.   Do you know if this works and what I might be doing wrong?  Does the Ftp support the full list of ftp commands? I have included the code below.

    Thanks,

    using System;
    using System.Net;
    using System.Text;
    using System.IO;
    using OpenNETCF.Net;
    using OpenNETCF.Net.Ftp;
           
     private static FtpWebRequest request;
            private static Stream ftpRequestStream;

            public bool DisplayFileFromServer()
            {
                StringBuilder result = new StringBuilder();
                try
                {               
                    FtpRequestCreator creator = new FtpRequestCreator();
                    FtpWebRequest.RegisterPrefix("ftp:", creator);

                    IPAddress ip = IPAddress.Parse("10.16.48.18");

                    Uri serverUri = new Uri("ftp://" + ip + "/");

                    request = (FtpWebRequest) FtpWebRequest.Create(serverUri);
                    request.Binary = true;
                   
                    request.Credentials = new NetworkCredential("anonymous", "1");
                    request.Method = WebRequestMethods.Ftp.ListDirectory;

                    ftpRequestStream = request.GetRequestStream();               
                    FtpWebResponse response = (FtpWebResponse)request.GetResponse();
                   
                    StreamReader streamReader = new StreamReader(response.GetResponseStream());
                   
                    string line = streamReader.ReadToEnd();
                    while (line != null)
                    {
                        result.Append(line);
                        result.Append("\n");
                        line = streamReader.ReadLine();
                    }

                    result.Remove(result.ToString().LastIndexOf('\n'), 1);

                    streamReader.Close();
                    //response.Close();
                   
                    return true;
                }
                catch ( Exception e)
                {
                    return false;
                }

            }
     

     

  • 10-19-2011 9:28 In reply to

    Re: FTP connection WebRequestMethods.Ftp.ListDirectory

    I had figured it out.  There is a good example at :

    http://community.opennetcf.com/forums/p/4126/65186.aspx

     

    Thanks,

  • 10-19-2011 9:58 In reply to

    Re: FTP connection WebRequestMethods.Ftp.ListDirectory

     Hi.

    You can download a free and simple ftp.dll library for .net on www.acaliaro.it under "download" section

Page 1 of 1 (3 items)