472,353 Members | 994 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,353 software developers and data experts.

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 58640
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Daniel Billingsley | last post by:
The documentation on this subject seems to give you 98% of what you need to know. From various sources, I have compiled an understanding that this...
9
by: Rajat Tandon | last post by:
Hello there, I am relatively new to the newsgroups and C#. I have never been disappointed with the groups and always got the prompt replies to my...
5
by: Rob | last post by:
Hi to all. I am new to Visualbasic.net and windowz programming in general so I hope someone can help. I am trying to get info from a csv (Excell)...
1
by: juventusaurabh | last post by:
Hi, Has anyone managed executing a DTS package in SQL Server 2000 from a windows form created using c#? Also, I specify the path of the new file...
13
by: cj | last post by:
In a project done in 2003 about a year ago I was told to add the SocketWrench code below into the Windows Form Designer generated code area as...
1
by: RSH | last post by:
Hi, I have a silly question... I have a Windows Form project (VB.Net) that was created in Visual Studio 2003. The project runs great. Now...
1
by: trinityofsouls | last post by:
I am trying to run a windows application from a service. Both the Windows Form App and the Windows Service were created using the .NET 2005 "New...
5
by: stoogots2 | last post by:
I have a Windows Form app in which a Windows Timer (when it ticks) starts a new thread to do a long running process. ...
2
by: danphillips1977 | last post by:
Hopefully this hasn't been asked already - apologies if so I have a Windows Form running inside C# - built using the form designer I've built...
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.