469,280 Members | 2,349 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,280 developers. It's quick & easy.

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

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

Thanks,
atlantix
Nov 15 '05 #1
3 58255
Look at class System.Diagnostics.Process, method Start().

Eliyahu

"atlantix" <at******@austarmetro.com.au> wrote in message
news:04****************************@phx.gbl...
How do you execute a .BAT file from a C# WinForm?
An example would be greatly appreciated.

Thanks,
atlantix

Nov 15 '05 #2
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" <re*************@monarchmed.com>
References: <04****************************@phx.gbl>
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

"atlantix" <at******@austarmetro.com.au> wrote in message
news:04****************************@phx.gbl...
How do you execute a .BAT file from a C# WinForm?
An example would be greatly appreciated.

Thanks,
atlantix


--

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.

Nov 15 '05 #3
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 [MSFT]" <bz***@online.microsoft.com> wrote in message
news:e%***************@cpmsftngxa06.phx.gbl...
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" <re*************@monarchmed.com>
References: <04****************************@phx.gbl>
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

"atlantix" <at******@austarmetro.com.au> wrote in message
news:04****************************@phx.gbl...
How do you execute a .BAT file from a C# WinForm?
An example would be greatly appreciated.

Thanks,
atlantix

--

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.

Nov 15 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Daniel Billingsley | last post: by
1 post views Thread by juventusaurabh | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.