Running a .BAT file from a Windows Form

What is a .Bat file?

execute bat file

A batch(.bat) file contains a series of DOS commands, and is commonly written to automate frequently performed tasks. These files (.bat) can save time by automating specific actions taken on the computer down to one simple click.

How to create a .Bat file?

In order to create a simple batch file, all you need is a single command you want to run, typed into a text file and saved with the .BAT extension, like "testbat.bat".

The following .bat file create a folder "d:\MyDir" in your D drive. The content of the file as follows :

ECHO Start Creating Directory MKDIR D:\MyDir ECHO Directory created D:\MyDir
execute bat file from C# or vb.net

Open a notepad and copy and paste the above content in the notepad and save it as "testbat.bat".

If you want to execute the file directly, double click the file (testbat.bat). Then you can see a new directory in your D drive named "MyDir".

In some situations you have to run .bat files from your C# or VB.Net applications. The following programs shows how to run a .bat file from C# or VB.Net.


Source Code | C#
using System; using System.Collections.Generic; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Diagnostics; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { Process proc = null; try { string batDir = string.Format(@"D:\"); proc = new Process(); proc.StartInfo.WorkingDirectory = batDir; proc.StartInfo.FileName = "testbat.bat"; proc.StartInfo.CreateNoWindow = false; proc.Start(); proc.WaitForExit(); MessageBox.Show("Bat file executed !!"); } catch (Exception ex) { Console.WriteLine(ex.StackTrace.ToString()); } } } }

Source Code | Vb.Net
Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim proc As Process = Nothing Try Dim batDir As String = String.Format("D:\") proc = New Process() proc.StartInfo.WorkingDirectory = batDir proc.StartInfo.FileName = "testbat.bat" proc.StartInfo.CreateNoWindow = False proc.Start() proc.WaitForExit() MessageBox.Show("Bat file executed !!") Catch ex As Exception Console.WriteLine(ex.StackTrace.ToString()) End Try End Sub End Class

Start and Kill Processes

Using the Process component, you can obtain a list of the processes that are running, or you can start a new process in the system. The System.Diagnostics namespace provides classes that allow you to interact with system processes, event logs, and performance counters. More about..... Start and Kill Processes

How get Running Process List

How get Running Process List from C# or vb.net

The Process class provides functionality to monitor system processes across the network, and to start and stop local system processes. You can retrieve detailed knowledge of process threads and modules both through the Process class itself. More about...... Running Process List



NEXT.....Rounding to 2 Decimal Places