Running a batch file from C#

Posted on 2010-01-08
Last Modified: 2012-05-08
I am trying to run a batch file from a c# console app. The file does a simple ftp get.

The batch file runs fine when run normally (double clicking in windows or from a command line), but when called through the c# it errors. This is my c#:

 System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo(@"C:\tmp\StaticUpdate.bat");
            psi.RedirectStandardOutput = true;
            psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
            psi.UseShellExecute = false;
            System.Diagnostics.Process listFiles;
            listFiles = System.Diagnostics.Process.Start(psi);
            System.IO.StreamReader myOutput = listFiles.StandardOutput;

Although I think :

System.Diagnostics.Process.Start(@"C:\tmp\StaticUpdate.bat"); all that is needed but the longer way lets me capture the error.

The batch file looks like this:

ftp -i -s:script.txt

Notice that it references script.txt. This contains the login details and commands for the FTP. However an error occurs

"Error opening script file script.txt". This only happens when run through the c#.

I've given 'everyone' full permissions on the folder so there shouldn't be an issue there.

This is the content of script.txt:
get c:\tmp\

The listfiles object in the c# is full of System.InvalidOperationException exceptions.

Any help would be great


Question by:hendrix500
    LVL 7

    Expert Comment

    can you put the script in the same location as you console app.the resason might be that it is not finding the script.txt it will be searching in the folder where your console.exe is.
    LVL 7

    Expert Comment

    sorry when i say script it means script.txt not the bat file as you are giving the path of bat
    LVL 7

    Accepted Solution

    or you can give the entire path of the script.txt in your batch file.
    LVL 1

    Author Closing Comment

    Easiest points you'll ever earn! Should have spotted that one. Thanks

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Join & Write a Comment

    After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
    Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA.…

    733 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now