JIYIK CN >

Current Location:Home > Learning > OPERATING SYSTEM >

Running batch scripts in C#

Author:JIYIK Last Updated:2025/03/21 Views:

In this article, we will see how to write a C# program that can run a batch file from a directory.


Running batch scripts in C#

In C#, when we want to execute a batch file, it acts as a process. You can follow the sample code below to run a batch script using a C# program.

System.Diagnostics.Process pros = new System.Diagnostics.Process();
pros.StartInfo.FileName = "C:\\MyDir\\simple.bat";
pros.StartInfo.WorkingDirectory = "C:\\MyWorkDir";
pros.Start();

In the sample code above, we execute a batch script called simple.bat. Here, you need to set the working directory before starting the process.

The above example is a code snippet that can run a batch file from a specified directory. In the following code snippet, we quickly perform the same task.

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 BatchLoader
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void Button_Click(object sender, EventArgs e)
        {
            // initialize empty process
            Process pros = null;
            try
            {
                string BatFileDir = string.Format(@"D:"); // directory of the file
                pros = new Process();
                pros.StartInfo.WorkingDirectory = BatFileDir;
                pros.StartInfo.FileName = "Mybat.bat"; // batch file name to be execute
                pros.StartInfo.CreateNoWindow = false;
                pros.Start(); // run batch file
                pros.WaitForExit();
                MessageBox.Show("Batch file successfully executed !!");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.StackTrace.ToString());
            }
        }
    }
}

First, we initialize all the required packages for our code. Then we initialize all the graphics components.

We provide an action to run the batch file via a button. With Process pros = null;the line, we initialize an empty process.

We keep the main part of the code in the exception handler because it may generate a runtime error. With the line string BatFileDir = string.Format(@"D:");we get a string containing the directory of the file.

After that, we declare a new process and initialize the working directory using the variable BatFileDir. We set the file name with the line pros.StartInfo.FileName = "Mybat.bat"; and pros.StartInfo.CreateNoWindow = false;disable opening new windows with the line.

We then pros.Start();execute the batch file with the line . The line pros.WaitForExit();tells the program to wait until the batch file is finished executing.

Finally, we MessageBox.Show(“Batch file successfully executed !!”);display a message to the user that the batch file has been successfully executed by .

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Extract or unzip files in batch script

Publish Date:2025/03/21 Views:122 Category:OPERATING SYSTEM

In this article, we will see how to create a batch script to extract files from a zip file. Create a batch script to unzip the files We can extract or decompress files using batch scripts which need to contain the destination directory wher

Transferring files to Raspberry Pi using batch script

Publish Date:2025/03/21 Views:56 Category:OPERATING SYSTEM

This article will show a batch command that transfers a file from Windows to the Raspberry Pi environment and executes the file. PuTTY is a third-party free tool that implements Telnet and SSH for Windows and Unix platforms for free. It als

Renaming part of a file name in a batch script

Publish Date:2025/03/21 Views:122 Category:OPERATING SYSTEM

Sometimes we need to rename a series of files in a specific order. Most project files contain this sequence, and we can easily do this in a batch script using a simple one-line command. This short article will show us how to change a specif

Run batch scripts using Task Scheduler

Publish Date:2025/03/21 Views:152 Category:OPERATING SYSTEM

This article will show you how to use Task Scheduler to run a batch file. Run batch scripts using Task Scheduler With Task Scheduler, you can automate tasks to run automatically at specific times. It only takes a few steps and you don't nee

Mapping a network drive in a batch script

Publish Date:2025/03/21 Views:69 Category:OPERATING SYSTEM

This article will discuss how to map a network drive in a batch script. Mapping a network drive in a batch script For this purpose, we will see three formats of the same command. However, the general format of the command is: net use P: "\\

Copy files using batch script

Publish Date:2025/03/21 Views:91 Category:OPERATING SYSTEM

Batch scripts are scripts built specifically for the Windows command line to perform a variety of tasks and execute user-directed sequences of commands. Batch scripts make it easy to use Windows PowerShell. This article will explain how to

Downloading files from URL in batch script

Publish Date:2025/03/21 Views:63 Category:OPERATING SYSTEM

Today we have several download managers to download necessary files from the Internet. In addition, there is a download manager integrated into every web browser. But you can create a batch script that can also perform a similar task of dow

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial