Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Need FTP Class
Message
 
To
16/06/2011 22:58:02
General information
Forum:
ASP.NET
Category:
Third party products
Title:
Miscellaneous
Thread ID:
01514847
Message ID:
01514908
Views:
72
>Kevin
>
>Do you want to FTP files from your program?
>
>If so, I use FTPWebRequest which is built into .NET.
>The documentation is pretty clear.
>Unfortunately, my classes contain confidential client information so I can't share them.
>If you have any questions, let me know.

I tried to write my own, but when I try to get a list of files from my FTP site, all I get is the root folder. Here's my code:
public struct FTPCredentials
{
    public string FTPServer { get; set; }
    public string UserName { get; set; }
    public string Password { get; set; }
}

public class FTP
{
    public string[] GetFileList(FTPCredentials Credentials)
    {
        string[] retVal = null;
        StringBuilder result = new StringBuilder();
        WebResponse response = null;
        StreamReader reader = null;

        try
        {
            FtpWebRequest reqFTP;

            reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(Credentials.FTPServer));
            reqFTP.UseBinary = true;
            reqFTP.Credentials = new NetworkCredential(Credentials.UserName, Credentials.Password);
            reqFTP.Method = WebRequestMethods.Ftp.ListDirectory;
            reqFTP.Proxy = null;
            reqFTP.KeepAlive = false;
            reqFTP.UsePassive = false;

            response = reqFTP.GetResponse();
                
            reader = new StreamReader(response.GetResponseStream());
            string line = reader.ReadLine();
            while (line != null)
            {
                result.Append(line);
                result.Append("\n");
                line = reader.ReadLine();
            }
            // to remove the trailing '\n'
            result.Remove(result.ToString().LastIndexOf('\n'), 1);
            return result.ToString().Split('\n');
        }
        catch (Exception ex)
        {
            if (reader != null)
            {
                reader.Close();
            }
            if (response != null)
            {
                response.Close();
            }
            return null;
        }

        return retVal;
    }
}
and I use it like this:
FTPCredentials credentials = new FTPCredentials
{
    FTPServer = "ftp://ftp.maroisconsulting.com",
    UserName = "FTPTest",
    Password = "testftp"
};

FTP ftp = new FTP();
string[] fileInfo = ftp.GetFileList(credentials);
It's not done. Still need to convert the results into an array. But all I get from this is the root folder. The FTP site and settings are valid if anyone wants to try it. There's a folder there called Test with 2 files in it.

Thanks
Everything makes sense in someone's mind
public class SystemCrasher :ICrashable
In addition, an integer field is not for irrational people
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform