Run Batch Files From Csharp In Dotnet Core CSharp DotNetCore

Jan 27th, 2018 - written by Kimserey with .

Being able to run batch during the lifecycle of an application is always useful. It gives a way to programmatically interact with any programs which implements a CLI. Today we will see how we can start processes from C# on .NET Core applications and how it can be useful in a real scenario.

  1. Unzip from CLI
  2. Use CLI from C#

1. Unzip from CLI

In this tutorial we will use a process to execute a 7zip command to extract files into a particular input.

If we have 7zip installed and added to PATH, using a terminal, we should able to execute the following from the folder containing the archive:

1
7z x .\\archive.zip -o.\\archive

x stands for extract and -o stands for output directory. The result of this command should be the list of files in extract unzipped into the archive folder.

There are times where it comes handy to zip and unzip as part of an application lifecycle, for example to allow a user to download multiple files from our server, we could zip them and push the zip through HTTP response. But to do that we need to run the command from C#.

2. Use CLI from C#

DotNet Core comes with a Process API which can be used to create and execute processes.

We start first by creating a process:

1
var process = new Process();

Then we set the start parameters like the file name, the string arguments:

1
2
process.StartInfo.FileName = "7z";
process.StartInfo.Arguments = "x .\\archive.zip -o.\\archive"

We also set it as such as the process does not create any window and we set it to no use ShellExecute:

1
2
process.StartInfo.CreateNoWindow = true;
process.StartInfo.UseShellExecute = false;

Setting UseShellExecute to false allows to redirect standard output and error. Setting it to true would be used to open files like .html files with default browser or .txt with notepad. For example the following code opens directly the zip file with your default program for zip:

1
2
3
4
var process = new Process();
process.StartInfo.UseShellExecute = true;
process.StartInfo.FileName = ".\\archive.zip";
process.Start();

In our case we just want to execute a command in the background and redirect the output so we set it to false. To redirect the output we can either use a delegate like so:

1
2
3
4
5
6
7
8
9
process.StartInfo.RedirectStandardOutput = true;
process.OutputDataReceived += (sender, data) => {
    Console.WriteLine(data.Data);
};

process.StartInfo.RedirectStandardError = true;
process.ErrorDataReceived += (sender, data) => {
    Console.WriteLine(data.Data);
};

Lastly we simply start the process:

1
process.Start();

And that’s it, we can now run our command from C#. As we can imagine, we could expose this from a Console App or from an ASP .Net Core api endpoint.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
var process = new Process();
process.StartInfo.FileName = "7z";
process.StartInfo.Arguments = "x .\\archive.zip -o.\\archive"
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.RedirectStandardOutput = true;
process.OutputDataReceived += (sender, data) => {
    Console.WriteLine(data.Data);
};
process.StartInfo.RedirectStandardError = true;
process.ErrorDataReceived += (sender, data) => {
    Console.WriteLine(data.Data);
};
process.Start();

Conclusion

Today we saw how we could run a process from C# where we could execute an extraction of a zip file using 7z. Hope this post was helpful! See you next time!

Designed, built and maintained by Kimserey Lam.