Introduction

This article is targeted for mid-level programmers who have already worked on C# language. This article uses batch files for demonstration and the reader should have preliminary knowledge of batch files.

Technology Used

Sample application describes how to call any Windows application or batch file using a C# console application. This application launches a new process and executes Windows applications in that process.

Demonstration of passing parameters to the batch file and accessing passed parameters in a batch file

API Used

System.Console.WriteLine()

System.Diagnostics.Process

Process.Start()

Using the code

Step by Step process to create a sample application

Step 1

Create a Console project called as CallBatchFile.

Your directory structure created would be like this after clicking the OK button:

Explanation about code

targetDir is a local variable which stores the directory path for the batch file to execute. ProcessStartInfo class (p.StartInfo) has workingDirectory, FileName, Arguments, and CreateNoWindow properties for the new process. Start() function will start the process. WaitForExit function will make sure that process will wait till it finishes its processing.

Configuration: Create a new directory called as BatchFile to the patch c:\temp\CallBatchFile. Your new directory structure would be like below:

Step 6

Create a new batch file called as MyBatchFile.bat and add the following lines:

@echo off
Echo Hello word ! From %1
Pause

First line is for Echo off which will make sure that the batch file will not print each line on the screen while file is executing. Second line will print Hello world ! with %1 (the parameter passed to the batch file). Third line is to pause the execution of the batch file.

Step 7

Compile the program and execute it. You should see the following result:

There are two windows created: one for the console application called CallBatchFile.exe and another for the new process where we are trying to execute the batch file.

Note: Remember we have earlier used p.StartInfo.CreateNoWindow=false, but still the new window is coming. This is because the batch file has pause command and it needs user's intervention to continue the execution.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

Expertiesed on different industry sectors of software development such as system softwares (worked for Intel and Microsoft ),application softwares (Manufacturing,Financial,Security,Airline industry, Multimedia)

Site developer for religious community in his spare time http://www.trimbakeshwar.net

I wanted to know how can you pass more than one parameter to the bat file using the startinfo.arguments statement.I have tried it with spaces between the 2 parameters on both C# and Vb.net but without any luck.Any help wud be appreciated.

If you are not interested in knowing when your spawned application has ended, invoke the ShellExecute function to run any file target, that is, if executable it will run, or if it is a registered file type, it will be run by registered handler application. Then you need not hardcode any application such as notepad.

I am looking for a way to call a console application (but to hide the console rather than have it popup when called, if possible) and pipe it's output to the calling program like a windows GUI form--so I can display in the GUI instead.

Then I came across this informative article. Is there a way to do what I want? If so, can someone show me the coding technique?

Just curious if you ever figured out how to call the console app from within a web app. I need to do the same thing...

If I'm not mistaken, you can do that in a web app. Works the same with other technologies like Perl, PHP, Java, and old/classic ASP.

The only drawback is you don't get any real-time feedback, meaning no console apps that prompt for input after execution or popup windows, etc. Thus calling GUI/windows apps from web app won't work either. But it is ok for console apps that just need command line parameters or none at all. And you can pipe the output to your webapp as mentioned in the forum thread "Re: hidding called application and piping output" in this article.

So you could, for instance, call "ver" to get windows OS version or "ipconfig /all" to get network settings, from your web app and display the results in a textbox.

"A good scientist is a person with original ideas. A good engineer is a person who makes a design that works with as few original ideas as possible." - Freeman Dyson