.NET Tip: Execute Commands From C# Hillsborough NC

Have you ever needed to execute a system command from your application? You can accomplish this easily with C#.

Local Companies

CINCOM Communications, Inc
702-943-0268
3415 N. Duke St.
Durham, NC
Quest Computer Products
(919) 644-6593
410 Millstone Dr Ste A
Hillsborough, NC
Kreimer Computing
(919) 245-0530
901 Chandler Ct
Hillsborough, NC
It Enterprises
(704) 568-6863
3801 E Independence Blvd
Charlotte, NC
Acuitive Solutions Inc
(704) 847-4997
1400 Highland Ridge Ct
Weddington, NC
Bloodhound Software
(919) 991-1117
2520 Meridian Pkwy
Durham, NC
Aten Inc
919.341.0441
104 T.W.Alexander Dr,
Raleigh, NC
All About Computers
(919) 245-0709
1000 Timber St
Hillsborough, NC
Interactive Learning Systems Inc
(919) 644-6322
3828 Saint Marys Rd
Hillsborough, NC
Eat Inc
(704) 329-0766
3809 Beam Rd Ste L
Charlotte, NC

provided by: 
Originally published at Internet.com


I usually run across the need to execute commands or run applications during the installation or configuration of an application. I've found this technique very useful in place of using batch files. You have a much more robust environment to work with and you don't have to worry about someone modifying your batch file. For this example, I'm just going to show you how execute a command that you would normally use in a Command Prompt. I normally include a function like the following in a static utility class so that it is accessible from anywhere in the application. public static int ExecuteCommand(string Command, int Timeout) { int ExitCode; ProcessStartInfo ProcessInfo; Process Process; ProcessInfo = new ProcessStartInfo("cmd.exe", "/C " + Command); ProcessInfo.CreateNoWindow = true; ProcessInfo.UseShellExecute = false; Process = Process.Start(ProcessInfo); Process.WaitForExit(Timeout); ExitCode = Process.ExitCode; Process.Close(); return ExitCode; }

The key here is the ProcessStartInfo class that gives you access to a process to execute your command. Here, I am telling the process not to create a window and to use the operating system shell to start the process. There are many more options available for ProcessStartInfo, so make sure you check them out to see what you can take advantage of in your application. In this, case I'm telling the process to execute cmd.exe. The /C parameter tells cmd.exe to execute the following string and then terminate. Again, cmd.exe has many more options that you should explore. The code then starts the process and waits for it to wither complete or time out. Finally, the exit code of the process is returned from the function.

I hope that you can see how easy it is to execute commands from within your application and that this may spark additional ideas of how you can enhance your applications.

About the Author

Jay Miller is a Software Engineer with Electronic Tracking Systems, a company dedicated to robbery prevention, apprehension, and recovery based in Carrollton, Texas. Jay has been working with .NET since the release of the first beta and is co-author of Learn Microsoft Visual Basic.Net In a Weekend. Jay can be reached via email at jmiller@sm-ets.com.

Author: Jay Miller

Read article at Internet.com site

Featured Local Company

Bloodhound Software

(919) 991-1117
2520 Meridian Pkwy
Durham, NC

Related Local Events
North Carolina Legislative Holiday Reception (Govt. Affairs)
Dates: 12/3/2009 - 12/3/2009
Location: Washington Duke Inn & Golf Club
Durham, NC
View Details

North Carolina Legislative Holiday Reception (Govt. Affairs)
Dates: 12/3/2009 - 12/3/2009
Location: Washington Duke Inn & Golf Club
Durham, NC
View Details

Breakfast & Business (PWN)
Dates: 11/24/2009 - 11/24/2009
Location: Durham Chamber
Durham, NC
View Details

Breakfast & Business (PWN)
Dates: 11/24/2009 - 11/24/2009
Location: Durham Chamber
Durham, NC
View Details

Lunch & Learn (SBC)
Dates: 11/11/2009 - 11/11/2009
Location: Durham Chamber
Durham, NC
View Details