Running a batch file from C#
Posted on 2010-01-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;
listFiles = System.Diagnostics.Process.Start(psi);
System.IO.StreamReader myOutput = listFiles.StandardOutput;
Although I think :
...is all that is needed but the longer way lets me capture the error.
The batch file looks like this:
ftp -i -s:script.txt ftp.mysite.com
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 myfile.zip c:\tmp\Local.zip
The listfiles object in the c# is full of System.InvalidOperationException exceptions.
Any help would be great