I've been experimenting with using Visual C# to connect to a remote FTP server, and list the folders on the server. Ultimately, I'm trying to figure out how to automatically upload a file to this server with the click of a button. I figured my first step would be to see how the folders are structured on the remote FTP server. However, when I try to view the results passed in debug, it appears to be empty, although I didn't receive any errors from the process, and the parameters in debug make it look like I was successfully logged in. I also received the file transfer complete message 226 in the response data. Here's the latest code I have tried, although I have tried various things in the FtpWebRequest function. If I turn off UsePassive, it sits there forever and I never get a response back from the server. Does anyone have any ideas on a setting that might be causing this or some parameter that might need to be set?
private void button4_Click(object sender, EventArgs e)
{
FtpWebRequest ftpRequest = (FtpWebRequest)WebRequest.Create("ftp://sampleftpplace.com/");
ftpRequest.UseBinary = true;
ftpRequest.Credentials = new NetworkCredential("username", "password");
ftpRequest.EnableSsl = true;
ftpRequest.Method = WebRequestMethods.Ftp.ListDirectory;
ftpRequest.Proxy = null;
ftpRequest.Timeout = -1;
ftpRequest.KeepAlive = false;
//ftpRequest.UsePassive = false;
FtpWebResponse response = (FtpWebResponse)ftpRequest.GetResponse();
StreamReader streamReader = new StreamReader(response.GetResponseStream());
List<string> directories = new List<string>();
string line = streamReader.ReadLine();
while (!string.IsNullOrEmpty(line))
{
directories.Add(line);
line = streamReader.ReadLine();
}
streamReader.Close();
}
When the "string line = streamReader.Readline();" is executed, the variable "line" is null. All I am really after is to see what folders are available so I can see how the original programmer of the FTP server structured their folders, and what naming convention they used. Is this possible if that programmer isn't available any more?
I just had a crazy thought, and it turned out that it worked! Basically, I changed the first couple of lines to read like this:
First, I used the command Uri. Not sure if that matters or not, but what really mattered was the 2nd "/" in the URL address. I think the server didn't see it as the root directory until I added this. Once I put that in, I'm seeing the folders now. Thanks!