Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Running a batch file from C#

Posted on 2010-01-08
4
Medium Priority
?
226 Views
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;
            listFiles.WaitForExit(2000);

Although I think :

System.Diagnostics.Process.Start(@"C:\tmp\StaticUpdate.bat");

...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:
MyUser
MyPass
get myfile.zip c:\tmp\Local.zip
quit

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

Any help would be great

Shaun

0
Comment
Question by:hendrix500
  • 3
4 Comments
 
LVL 7

Expert Comment

by:vikas_madhusudana
ID: 26210807
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.
0
 
LVL 7

Expert Comment

by:vikas_madhusudana
ID: 26210816
sorry when i say script it means script.txt not the bat file as you are giving the path of bat
0
 
LVL 7

Accepted Solution

by:
vikas_madhusudana earned 500 total points
ID: 26210853
or you can give the entire path of the script.txt in your batch file.
0
 
LVL 1

Author Closing Comment

by:hendrix500
ID: 31674579
Easiest points you'll ever earn! Should have spotted that one. Thanks
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
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. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…

810 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