Reputation: 39
I'm trying to write a program in C# that will FTP into a machine, download some logs, and then delete the logs off of the machine. This process works with a dev FTP server I have to test on my machine but when I try to connect to the production server I want this program to be used on I get a "550 No files found or invalid directory or permission problem" error on the server.
The server will list the directory file for FileZilla, even when using the exact same URI. I have captured the packets with wireshark and the server just cuts off after sending the text "total" to the data port when trying it with C#
Uri uri = new Uri("ftp://username:[email protected]:21/");
FtpWebRequest ftpWebRequest = FtpWebRequest)WebRequest.Create(uri.ToString());
ftpWebRequest.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
// Get the response
FtpWebResponse response = (FtpWebResponse)ftpWebRequest.GetResponse();
// Get the stream and reader
Stream responseStream = response.GetResponseStream();
StreamReader reader = new StreamReader(responseStream);
// Close the response
response.Close();
// Get all the results into a list
List<string> results = new List<string>();
results.AddRange(reader.ReadToEnd().Split('\n')); //<---- Web Exception Here
Upvotes: 1
Views: 1278
Reputation: 448
Please try it this way:
Uri uri = new Uri("ftp://username:[email protected]:21/");
FtpWebRequest ftpWebRequest = (FtpWebRequest)WebRequest.Create(uri.ToString());
ftpWebRequest.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
using (FtpWebResponse response = (FtpWebResponse)ftpWebRequest.GetResponse())
using (Stream responseStream = response.GetResponseStream())
using (StreamReader reader = new StreamReader(responseStream))
{
List<string> results = new List<string>();
results.AddRange(reader.ReadToEnd().Split('\n'));
}
In general it is safest to use using
when dealing with streams or other disposable resources.
Upvotes: 2