Trying to execute Batch files from a C# application

m_meuse1

Trying to execute Batch files from a C# application

by m_meuse1 » Sun, 04 Oct 2009 12:51:25

Hello all,

I am trying to exectute a simple batch file from a C# application and I am running into some trouble. Basically, my code compiles fine, but when I run it, the batch file is never executed. I am simply trying to run a code sample that I received from another website and I am curious to know if this is a coding or machine related issue. Your thoughts would be most appreciated. Here is the code snippet:

using System;

using System.Text;

using System.Diagnostics;

namespace CallBatchFile

{

class Program

{

static void Main(string[] args)

{

Process p = new Process();

try

{

string targetDir;

targetDir = string

p.StartInfo.UseShellExecute = true;

p.StartInfo.WorkingDirectory = targetDir;

p.StartInfo.FileName = "MyBatchFile.bat";

p.StartInfo.Arguments = string.Format("C-Sharp Console application");

p.StartInfo.CreateNoWindow = false;

p.Start();

p.WaitForExit();

}

catch (Exception ex)

{

Console.WriteLine("Exception Occurred :{0},{1}", ex.Message,ex.StackTrace.ToString());

}

}

}

}



ahmedilya

Trying to execute Batch files from a C# application

by ahmedilya » Tue, 06 Oct 2009 13:52:26

try setting the UseShelExecute to false and see what happens. Also how do you know it never executes Does it produce some result to indicate that its finished executing What does the batch file do You may wish to redirect the outputstream to a textbox for example in the ProcessStartInfo to see what it is outputting:

http://msdn2.microsoft.com/en-us/library/system.diagnostics.processstartinfo.aspx

http://msdn2.microsoft.com/en-us/library/system.diagnostics.processstartinfo.redirectstandardoutput.aspx



ahmedilya

Trying to execute Batch files from a C# application

by ahmedilya » Wed, 07 Oct 2009 15:54:28

well since the batch file has a pause, it waits for the user to press a key but the process is also waiting for the process to exit (the batch file) but it can't because its waiting for the user input (from the batch file)

what you may need to do is also implement the RedirectStandardInput as well as standard output so you can interact with the batch file inputs/outputs

AS you maybe aware, the batch file is not shown to the user as you have told the PSI (ProcessStartInfo) to not create a window for it. If you just want to run the batch file:

Process.Start("path\batchfile.bat");

and this will run the batch file as if you double clicked on it, or remove the CreateNoWindow line altogether. I'll see what else I can find




m_meuse1

Trying to execute Batch files from a C# application

by m_meuse1 » Thu, 08 Oct 2009 14:53:27

Hello ahmedilyas,

1. The batch file "MyBatchFile.bat" simply echos Hello World and waits for the user to exit the window. Here is the batch just for reference:


Echo Hello word ! From %1
Pause

2. Setting UseShellExecute = false, which is necessary to get RedirectStandardOutput, seems to give me a "The system cannot find the file specified" exception. The original code sample that I provided in my previous post when run, will just display "press any key to continue" in the console, making it seem that the batch file never runs. Nonetheless, here is the modified code snippet of interest that gives me the exception only when UseShellExecute = false:

static void Main(string[] args)

{

Process p = new Process();

try

{

string targetDir;

targetDir = string

p= new Process();

p.StartInfo.WorkingDirectory = targetDir;

p.StartInfo.FileName = "MyBatchFile.bat";

p.StartInfo.CreateNoWindow = false;

p.StartInfo.Arguments = string.Format("C-Sharp Console application");

p.StartInfo.UseShellExecute = false;

p.StartInfo.RedirectStandardOutput = true;

p.Start();

p.WaitForExit();

StreamReader se = p.StandardOutput;

string output = se.ReadToEnd();

Console.WriteLine("myProcess.StandardOut:" + output);

}

catch (Exception ex)

{

Console.WriteLine("Exception Occurred :{0},{1}", ex.Message,ex.StackTrace.ToString());

}

}

}

}

Your help and suggestions are most appreciated. I have never never actually called an external batch file from any program so this is a great learning experience.

Regards,

-Matt


m_meuse1

Trying to execute Batch files from a C# application

by m_meuse1 » Fri, 09 Oct 2009 16:55:29

Hey Ahmedilyas,

Thank you very much for your help. Frankly, I can really figure out what I'm not doing right. I stripped down all of the code and ran:

Process.Start("C:\Documents and Settings\mmeuse\Desktop\CallBatchFile\BatchFile\MyBatchFile.bat");

Process.Start("C:\Documents and Settings\mmeuse\Desktop\CallBatchFile\BatchFile\HelloWorld.exe");

Of course, the executable ran file, but the batch file is still not running as if I simply double-clicked MyBatchFile.bat on the desktop. Nonetheless, I think that I just do not understand enough about Processes/Creating Batch files in general so I will have to do some more research. Anyway, thank you for your help and if you have any tips as to where to search for some more info, that would be great (I'm on msdn as we speak). Thanks, and MS products are great!

Regards,

-Matt


m_meuse1

Trying to execute Batch files from a C# application

by m_meuse1 » Sat, 10 Oct 2009 18:57:31

Hey Ahmedilyas,

I just wanted to let you know that I was able to figure out why I wasn't able to execute .BAT files from my C# application, but I was successfully able to run .EXEs.

It turns out, that I had some security software installed on my machine that was preventing CMD.exe to run, when my C# application attempted to execute a particular .BAT file. To resolve the issue, I removed the security software, ran the app and my Batch files executed properly. So, it wasn't a coding issue at all, which is frustrating, because I (and you) spent some time trying to debug the issue. Nonetheless, I wan't to thank you for your help and I will definately have to be more "System Conscious" when developing in the future.

Cheers,

~Matt


ahmedilya

Trying to execute Batch files from a C# application

by ahmedilya » Sun, 11 Oct 2009 17:56:30

Do you have to put in the pause in the batch file Remember you won't get any output from it without having implemented or redirected the StandardOutputStream so you can read what the program/batch file has returned back to the caller/display.

so you can remove the pause in the batchfile, redirect the output stream as you did/given in the links and read the stream of output given from the batch file.



ahmedilya

Trying to execute Batch files from a C# application

by ahmedilya » Mon, 12 Oct 2009 19:58:32

doh!

no worries at all, it happens :-) Thanks for sharing the solution however