16
Apr 14

FTP File Upload with C#

I use the code below for uploading files to an FTP server. It worked in my development environment but as soon as I deployed my application to a Windows Azure VM it started giving me the following error:
System.Net.WebException: The remote server returned an error: (500) Syntax error, command unrecognized.

Basically, the call to GetRequestStream() method was causing the problem.

To enable System.Net tracing, I added the configuration to my app.config file. This would enable the application to dump necessary debugging messages into a file named “System.Net.trace.log”.

After the first run, I opened the .log file and found this message next to GetRequestStream() call:
500 I won’t open a connection to [my internal IP] (only to [external IP])

Somehow the FTP passive mode transfer was causing this issue. Setting request.UsePassive = true did the trick.

The System.Net tracing configuration that has been added to my app.config file:

The C# code for uploading a file to an FTP server: