Click here to Skip to main content
Click here to Skip to main content

Run a Command Line Tool as a Windows Application in C#

By , 8 Apr 2002
 

Sample Image

Introduction

It's almost always more convenient to work with a windows GUI other than have to use the DOS command prompt. It's easy to convert a DOS command line tool into a Windows like application in C# by simply adding some Windows Forms tools interacting with some code. 

Here I take the RegenResx.exe as an example. I built a simple windows interface so that I could easily convert old version resx files to be compatible with any version of the .NET framework. This command line tool is very useful when upgrading older version resource files. The tool itself can be found at RegenResx Conversion Tool, or Upgrading from the .NET Framework Beta Versions.  I use the System.Diagnostics namespace and the Process.Start Method. This simple approach could similarly be applied to run other command line tools, DOS commands, and other windows applications. Some examples are also provided in the demo project.

The demo project is a windows form application. A TabControl is put on a Windows form. Each of the three tabs demonstrates a simple situation, either to run a command line tool, a DOS command, or to run another windows application, such as launch Internet Explorer with given address.

Code Sample

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Diagnostics;

    ...

    //Declare and instantiate a new process component.
    System.Diagnostics.Process process1;
    process1= new System.Diagnostics.Process();

    //Do not receive an event when the process exits.
    process1.EnableRaisingEvents = false;


    //The "/C" Tells Windows to Run The Command then Terminate 
    string strCmdLine;
    strCmdLine = "/C regenresx "+textBox1.Text + " " +textBox2.Text;
    System.Diagnostics.Process.Start("CMD.exe",strCmdLine);
    process1.Close();

    ...

Summary

Take the command line tool RegenResx as an example, write the windows interface to convert old version resx files to be compatible with any version of the .NET framework. It's easy and fun to write simple useful application in C# using the powerful .NET Framework.

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.

A list of licenses authors might use can be found here

About the Author

shilin
China China
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 1memberMember 767178617 Feb '11 - 4:17 
test
GeneralMinimizingmembersoloforce11 Jun '09 - 2:52 
Hi great program.... Do you know of a way to minimize the command prompt when using this code?
GeneralPrint Word Doc using asp.net using System.Diagnostic.Processmembersamanthan sadralin17 Feb '05 - 2:47 
Hi all,
 
I'm able to print word doc using console application. I get the source code from http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemDiagnosticsProcessClassTopic.asp[^]
But how can I ask the website to print .doc file.
 
If u see this thread. Please do reply me. Your help is greatly appreciated.

Questionhow can I get..memberrethore5 Jun '04 - 0:51 
hi,
how can I get the informations written by the command line program inside the c# app?
thanks!
GeneralStart(string, string) is static methodmemberCraig Arnold11 Jun '02 - 1:33 
I stand to be corrected but...
 
I think you'll find that the code snippet:
 
string strCmdLine;
strCmdLine = "/C regenresx "+textBox1.Text + " " +textBox2.Text;
System.Diagnostics.Process.Start("CMD.exe",strCmdLine);
 
works fine by itself. You don't need the rest. This is because the Start() method is static. The process that you start and close isn't used for anything.
 
Nevertheless - I found this article useful, because it wasn't particularly intuitive (to me at least!) to look in the System.Diagnostics namespace to run a process. Smile | :)
 

GeneralRe: Start(string, string) is static methodmemberLoai16 Nov '02 - 11:01 
Yes Craig, the 'Process' initialized in the code was in fact never used anywhere and Start(string, string) would do the job.
 
My question, however, is how would I open a new process and continue to communicate with it. That is, I want to open the command-line (cmd.exe) and keep sending my commands to the same instance of it. In the first command I trigger a DOS application that can take arguments; I want to be able to keep feeding it. Any idea how I can do that?
 
Thank you,
L.
GeneralRe: Start(string, string) is static methodmemberJ0ker9 Sep '04 - 5:57 

ProcessStartInfo si = new ProcessStartInfo("cmd");
si.RedirectStandardInput = true;
si.RedirectStandardOutput = true;
si.RedirectStandardError = true;
si.UseShellExecute = false;
Process p = Process.Start(si);

now you can communicate with the process by streams p.StandardInput, p.StandardOutput and p.StandardError
GeneralRe: Start(string, string) is static methodmemberchubbysilk21 Sep '04 - 11:09 
Hello,
I would like to open a command prompt and continue executing commands throught it. I tried the following code:
 

ProcessStartInfo si = new System.Diagnostics.ProcessStartInfo("cmd.exe");
 
si.RedirectStandardInput = true;
si.RedirectStandardOutput = true;
si.RedirectStandardError = true;
si.UseShellExecute = false;
 
Process console = Process.Start(si);
console.StandardInput.WriteLine("cd\");

 
But it did not work. Could you give me an example of some working code?
 
Thanks,
RC
GeneralRe: Start(string, string) is static methodmembercdwilliams26 Sep '06 - 8:48 
Try this:
 
ProcessStartInfo si = new System.Diagnostics.ProcessStartInfo("cmd.exe");
 
si.RedirectStandardInput = true;
si.RedirectStandardOutput = false; // change this to false so you can what is happening
si.RedirectStandardError = false; // change this to false to see any errors
si.UseShellExecute = false;
 
Process console = Process.Start(si);
console.StandardInput.WriteLine("md c:\\testdir");
console.StandardInput.WriteLine("c:");
console.StandardInput.WriteLine("cd \\testdir");

GeneralRe: Start(string, string) is static methodmemberLava041 Mar '07 - 2:23 
This didnt work either
 
cdwilliams wrote:
si.RedirectStandardInput = true;
si.RedirectStandardOutput = false; // change this to false so you can what is happening
si.RedirectStandardError = false; // change this to false to see any errors
si.UseShellExecute = false;
 
Process console = Process.Start(si);
console.StandardInput.WriteLine("md c:\\testdir");
console.StandardInput.WriteLine("c:");
console.StandardInput.WriteLine("cd \\testdir");

 

 
Hamza

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130516.1 | Last Updated 9 Apr 2002
Article Copyright 2002 by shilin
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid