Running a .BAT file from a Windows Form in C#

  • Thread starter Thread starter atlantix
  • Start date Start date
A

atlantix

How do you execute a .BAT file from a C# WinForm?
An example would be greatly appreciated.

Thanks,
atlantix
 
Correct. Here is a sample code snippet you can paste into your Main function:

System.Diagnostics.ProcessStartInfo p = new System.Diagnostics.ProcessStartInfo(@"c:\foo.bat");
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo = p;

proc.Start();
proc.WaitForExit();

Console.WriteLine("I'm done");

Note: you might tweak it a little bit your code (or batch file) to allow/avoid async execution (it really depends on what you are trying to do).

-Matteo

--------------------
From: "Eliyahu Goldin" <[email protected]>
References: <[email protected]>
Subject: Re: Running a .BAT file from a Windows Form in C#
Date: Mon, 28 Jul 2003 10:44:34 +0200
Lines: 13

Look at class System.Diagnostics.Process, method Start().

Eliyahu


--

This posting is provided "AS IS" with no warranties, and confers no rights. Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm

Note: For the benefit of the community-at-large, all responses to this message are best directed to the newsgroup/thread from which they originated.
 
Hi,

Another couple of things that you need to be care of:
1- Create a pif file and check "Close on Exit" , otherwise you wil have a
DOS screen saying something like "FINISHED Program Name"
2- If you do not want that the DOS windows is shown select Normal as the
windows mode in the PIF's creation dialog ( also take a look at the code
below )

This is the code I'm using to exec a bat:
Process proc = new Process();

proc.StartInfo.FileName = foxexe;

proc.StartInfo.Arguments = prg;

proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;

proc.StartInfo.ErrorDialog = false;

proc.StartInfo.WorkingDirectory = Path.GetDirectoryName( prg);

proc.Start();

proc.WaitForExit();

if ( proc.ExitCode!= 0 )

throw new Exception("Error executing foxpro");


Hope this help,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation


Matteo Taveggia said:
Correct. Here is a sample code snippet you can paste into your Main function:

System.Diagnostics.ProcessStartInfo p = new System.Diagnostics.ProcessStartInfo(@"c:\foo.bat");
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo = p;

proc.Start();
proc.WaitForExit();

Console.WriteLine("I'm done");

Note: you might tweak it a little bit your code (or batch file) to
allow/avoid async execution (it really depends on what you are trying to
do).
rights. Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm

Note: For the benefit of the community-at-large, all responses to this
message are best directed to the newsgroup/thread from which they
originated.
 
Back
Top