Reputation: 1050
I need some help with some code that is not working for some reason. I'm making a method that gets a list of files in a FTP directory. Every time I debug the app, a WebException is thrown with the StatusCode of 530 (not logged in). Keep in mind that I am 100% positive the address, username, and password are correct. Here's the method:
public static List<string> GetFileList(string Directory)
{
List<string> Files = new List<string>();
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(new Uri(ServerInfo.Root + Directory));
request.Method = WebRequestMethods.Ftp.ListDirectory;
request.Credentials = new NetworkCredential(ServerInfo.Username, ServerInfo.Username);
FtpWebResponse response = (FtpWebResponse)request.GetResponse(); //Error occurs here
Stream responseStream = response.GetResponseStream();
StreamReader reader = new StreamReader(responseStream);
string CurrentLine = reader.ReadLine();
while (!string.IsNullOrEmpty(CurrentLine))
{
Files.Add(CurrentLine);
CurrentLine = reader.ReadLine();
}
reader.Close();
response.Close();
return Files;
}
This is the value of ServerInfo.Root: "ftp://192.xxx.4.xx:21/MPDS" (partially censored for privacy)
I have used MessageBoxes to ensure the complete URI is correct, and it is.
I've been struggling with this problem for a long time now, so I hope you can help me fix it.
Thanks in advance!
Upvotes: 1
Views: 10035
Reputation: 288
You can try this code with some corrections:
public static List<string> GetFileList(string Directory)
{
List<string> Files = new List<string>();
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(new Uri(ServerInfo.Root + Directory));
request.Method = WebRequestMethods.Ftp.ListDirectory;
request.Credentials = new NetworkCredential(ServerInfo.Username, ServerInfo.Username); // Is this correct?
// request.Credentials = new NetworkCredential(ServerInfo.Username, ServerInfo.Password); // Or may be this one?
request.UseBinary = false;
request.UsePassive = true;
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();
StreamReader reader = new StreamReader(responseStream);
string CurrentLine = reader.ReadLine();
while (!string.IsNullOrEmpty(CurrentLine))
{
Files.Add(CurrentLine);
CurrentLine = reader.ReadLine();
}
reader.Close();
response.Close();
return Files;
}
Upvotes: 2