We help IT Professionals succeed at work.

Ftp Command Error Handling in .NET?

1,021 Views
Last Modified: 2013-12-17
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
Comment
Watch Question

CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008

Commented:
If you are using the System.Diagnostics.Process class to execute the batch file, you can redirect the StandardOutput and/or the StandardError to get the necessary output.

Bob

Author

Commented:
do you have code examples?
Retired
CERTIFIED EXPERT
Distinguished Expert 2017
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
Are there free ftp servers on the web that I can run the code against?.
Fernando SotoRetired
CERTIFIED EXPERT
Distinguished Expert 2017

Commented:
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 ?

Author

Commented:
Is it possible to configure FTP on my local machine and make it work that way? If so, please show me how
Fernando SotoRetired
CERTIFIED EXPERT
Distinguished Expert 2017

Commented:
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/";

Author

Commented:
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,
Fernando SotoRetired
CERTIFIED EXPERT
Distinguished Expert 2017

Commented:
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.Message);
        err = null;
    }

Write to a log file so that any errors can be corrected by some one.

Author

Commented:
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?
Fernando SotoRetired
CERTIFIED EXPERT
Distinguished Expert 2017

Commented:
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("myusername", "mypassword");

Commented:
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
Fernando SotoRetired
CERTIFIED EXPERT
Distinguished Expert 2017

Commented:
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.

Commented:
FernandoSoto,

Thank you for your response. I created a new question with ID 23077135.

lanac

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.