Share via


C# running a batch file

Question

Tuesday, April 28, 2015 5:40 PM

ok I am trying to run a batch file with C#, but it is not working.

Now the batch file work, I have tested that.

but when the C# program runs the batch files.

The command prompt comes up, and I see letters like it is doing something but this it closes and I check and the batch file did not run.

here Is my code:

 String command =@"C:\uploader\upload.bat";
            Process test = new Process();
            try
            {
                test.StartInfo.FileName = command;
                test.StartInfo.RedirectStandardError = true;
                test.StartInfo.RedirectStandardOutput = true;
                test.StartInfo.UseShellExecute = false;
                test.Start();
                test.WaitForExit(150000);
            }
            catch (Exception e1)
            {
                MessageBox.Show(e1.Message);
            }
          
            MessageBox.Show( "Done");

All replies (8)

Tuesday, April 28, 2015 6:19 PM âś…Answered

      I'm doing something similar in a console app with the below code. 

  try
                {
                    string targetDir;
                    targetDir = string.Format(@"C:\MyStuff");   //directory where the batch script is saved.

                    p.StartInfo.UseShellExecute = true;
                    p.StartInfo.WorkingDirectory = targetDir;   
                    p.StartInfo.FileName = "test.bat";          //this is the batch script that needs to run.
                    p.StartInfo.CreateNoWindow = false;
                    p.Start();
                    p.WaitForExit();
                    System.Threading.Thread.Sleep(5000);        //Wait 5 seconds to close window.
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Exception Occurred :{0},{1}", ex.Message, ex.StackTrace.ToString());
                }


Tuesday, April 28, 2015 5:42 PM

Also I tried:

Process.Start(@"C:\uploader\upload.bat"); and it would run the batch file and not work as well can anyone help?


Tuesday, April 28, 2015 6:23 PM

Also I tried:

Process.Start(@"C:\uploader\upload.bat"); and it would run the batch file and not work as well can anyone help?

Since you state that the bat file runs, and the only code you have shown us is that which
invokes the bat file, how do you think we can help? Since we have no idea what is in the
bat file, how can we possibly tell you what may be wrong with it?

Also, how are you determining that it did "not work"? What is it supposed to do that it
isn't doing?

  • Wayne

Tuesday, April 28, 2015 6:56 PM

but when the C# program runs the batch files.

The command prompt comes up, and I see letters like it is doing something but this it closes and I check and the batch file did not run.

>Also I tried:
>Process.Start(@"C:\uploader\upload.bat"); and it would run the batch file and not work

In your bat file, make the last statement:

pause

That should keep the window open so you can check its contents.

Note that there may be *two* console windows open from your program, if you are running
from the IDE with a breakpoint and it's a console application.

  • Wayne

Tuesday, April 28, 2015 7:26 PM

SubzeroLV

did that work for you?


Tuesday, April 28, 2015 7:41 PM

wayne

the bat file program a device I have.

I know if it work, if the device is programed or not.

it works if I just run the bat file myself, but not when the C# programs runs it.


Tuesday, April 28, 2015 7:54 PM

I added pause to the end of it, but it did not work

What is "it"? Are you saying the pause did not work?

  • Wayne

Tuesday, April 28, 2015 8:38 PM

SubzeroLV

thanks

your code worked