tinman1412
asked on
Ftp Command Error Handling in .NET?
Hi All,
I have a .bat file that will call a text file with ftp commands. The commands basically uploads files from my local machine to a remote server. I was wondering is there is a way to catch and write errors if something did not process correctly?. How do I go about doing this. Can this be done in the ftp commands or should I write a C# program to handle the error. Please advise. Below is a quick example of what the ftp command looks like in the text file.
open 10.8.2.64
myusername
mypassword
cd uploadfolder
mput *.doc
close
quit
I have a .bat file that will call a text file with ftp commands. The commands basically uploads files from my local machine to a remote server. I was wondering is there is a way to catch and write errors if something did not process correctly?. How do I go about doing this. Can this be done in the ftp commands or should I write a C# program to handle the error. Please advise. Below is a quick example of what the ftp command looks like in the text file.
open 10.8.2.64
myusername
mypassword
cd uploadfolder
mput *.doc
close
quit
ASKER
do you have code examples?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Are there free ftp servers on the web that I can run the code against?.
Sorry I do not know of any you can test the code with. Can you not try the one you are working with 10.8.2.64 ?
ASKER
Is it possible to configure FTP on my local machine and make it work that way? If so, please show me how
If you have a system that supports IIS, "Internet Information Services", and it is installed on your system then yes. You would need to also enable FTP Services.
To see if it is installed go to the Control Panel -> Administrative Tools and see if you have a component called Internet Information Services. If you do open it up and see if it has FTP enabled and its running.
If you do not have it installed go to the Control Panel and double click on Add/Remove Programs. Once there click on Add or Remove Windows Components and see if "Internet Information Services" is a component available to be installed if yes and you wish to install it you will need the original CD's you loaded windows with to get it loaded.
If you have it loaded and configured the FTP URL for your machine will be ftp://localhost if you run an FTP client from the local computer to get access from another system you will need to get the IP address of your computer. To do that open a DOS Command Prompt window and enter the command ipconfig then the return key. The out put of that command will have a line that reads
IP Address. . . . . . . . . . . . : XXX.XXX.XXX.XXX
the XXX.XXX.XXX.XXX is the IP Address of your machine. So lets say that the IP Address is 10.0.50.1 then to make the code I posted work you would change
string remoteURI = "ftp://10.8.2.64/";
in the code to
string remoteURI = "ftp://10.0.50.1/";
To see if it is installed go to the Control Panel -> Administrative Tools and see if you have a component called Internet Information Services. If you do open it up and see if it has FTP enabled and its running.
If you do not have it installed go to the Control Panel and double click on Add/Remove Programs. Once there click on Add or Remove Windows Components and see if "Internet Information Services" is a component available to be installed if yes and you wish to install it you will need the original CD's you loaded windows with to get it loaded.
If you have it loaded and configured the FTP URL for your machine will be ftp://localhost if you run an FTP client from the local computer to get access from another system you will need to get the IP address of your computer. To do that open a DOS Command Prompt window and enter the command ipconfig then the return key. The out put of that command will have a line that reads
IP Address. . . . . . . . . . . . : XXX.XXX.XXX.XXX
the XXX.XXX.XXX.XXX is the IP Address of your machine. So lets say that the IP Address is 10.0.50.1 then to make the code I posted work you would change
string remoteURI = "ftp://10.8.2.64/";
in the code to
string remoteURI = "ftp://10.0.50.1/";
ASKER
Ok, I currently have a batch file that calls a text file with the ftp commands to put files onto the remote machine via a windows scheduler. My problem is I'm not able to catch the errors if something occurs. Based on your code, are you suggesting that I basically write a Windows service .exe and run it from the scheduler?. This way, I would no longer need the batch file and the ftp text file with the commands, I would just use .NET managed code and the FTP object in .NET to transfer the files?
Thanks,
Thanks,
That is correct. Then in this section of code:
if (err != null)
{
// Write the error somewhere and reset err to null for the next transfer
Console.WriteLine(err.Mess age);
err = null;
}
Write to a log file so that any errors can be corrected by some one.
if (err != null)
{
// Write the error somewhere and reset err to null for the next transfer
Console.WriteLine(err.Mess
err = null;
}
Write to a log file so that any errors can be corrected by some one.
ASKER
I noticed you are using the "WebClient" class. Will this work. Is there not an FTP class in .net 2005 that can be used. Basically, it will be an ftp site with has a specific username/password credential setup. Does the "WebClient" do this as well?
To your question, "Will this work.", Yes it will I have tested the code before posting as is my normal habit.
To your question, "Is there not an FTP class in .net 2005 that can be used.", The WebClient supports the following protocols, http:, https:, ftp:, and file: scheme identifiers. This is the .Net FTP class. The function that is used is determined by the URL that is given to the WebClient.
To your question, "Basically, it will be an ftp site with has a specific username/password credential setup. Does the "WebClient" do this as well?", The FTP methods of WebClient support username/password when it uses the NetworkCredential class as shown below which I have posted in my solution.
// The user name and password are stored here
NetworkCredential nc = new NetworkCredential("myusern ame", "mypassword");
To your question, "Is there not an FTP class in .net 2005 that can be used.", The WebClient supports the following protocols, http:, https:, ftp:, and file: scheme identifiers. This is the .Net FTP class. The function that is used is determined by the URL that is given to the WebClient.
To your question, "Basically, it will be an ftp site with has a specific username/password credential setup. Does the "WebClient" do this as well?", The FTP methods of WebClient support username/password when it uses the NetworkCredential class as shown below which I have posted in my solution.
// The user name and password are stored here
NetworkCredential nc = new NetworkCredential("myusern
FernandoSoto,
I tried your code and I have valid FTP server. I got error 'An exception occurred during a WebClient request.' What that could be? I may add some points to if needed.
Thanks.
lanac
I tried your code and I have valid FTP server. I got error 'An exception occurred during a WebClient request.' What that could be? I may add some points to if needed.
Thanks.
lanac
That would be hard to tell without having the full error message. But this question has been closed and anyone needing help will need to open a new question.
Thank you.
Thank you.
FernandoSoto,
Thank you for your response. I created a new question with ID 23077135.
lanac
Thank you for your response. I created a new question with ID 23077135.
lanac
Bob