.NET Tip: Execute Commands From C# Wisconsin

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

Local Companies

IFS
414-577-5191
12000 W. Park Place
Milwaukee, WI
HarrisData
262-784-9099
13555 Bishop's Court, Suite 300
Brookfield, WI
Electroniclaim
262-240-9700
11357 N. Port Washington Rd
Mequon, WI
R.E. Coker and Associates, Inc.
262-723-8104
108 W Court St.
Elkhorn, WI
Acumium
608 310 9700 x 522
5133 West Terrace Drive Suite 300
Madison, , WI
Xorbix Technologies Inc.
414-277-5044
759 N. Milwaukee St.
Milwaukee, WI
R S InfoCon, Inc.
262-898-7456
2320 Renaissance Blvd
Sturtevant, WI
Visionary Computer Solutions
262-365-9430
PO Box 406
Grafton, WI
HarrisData
800-225-0585
13555 Bishops Court
Brookfield, WI
Wireless Direct
1.866.707.8498
BOX 71101
shorewood, WI


.NET Tip: Execute Commands From C#

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

Wireless Direct

1.866.707.8498
BOX 71101
shorewood, WI
http://www.prepaidwireless.2ya.com

Related Articles
- Programming MIDlets for Interactive Behavior Wisconsin
Discover how to add commands to a MIDlet and how those commands are presented to the user in the Sun cell phone emulator. You will also learn how to produce interactive behavior within a MIDlet.
- HipCheck Administrative Command Wisconsin
- Working With Design Patterns: Memento Wisconsin
- Working with Design Patterns: Command Wisconsin
- Drilling Into SQL Server Management Studio Wisconsin
- Secrets of the MySQL Client Command Line Wisconsin
Regional Articles
- .NET Tip: Execute Commands From C# Appleton WI
- .NET Tip: Execute Commands From C# Baraboo WI
- .NET Tip: Execute Commands From C# Beaver Dam WI
- .NET Tip: Execute Commands From C# Beloit WI
- .NET Tip: Execute Commands From C# Brookfield WI
- .NET Tip: Execute Commands From C# Burlington WI
- .NET Tip: Execute Commands From C# Cedarburg WI
- .NET Tip: Execute Commands From C# Chippewa Falls WI
- .NET Tip: Execute Commands From C# Cudahy WI
- .NET Tip: Execute Commands From C# De Pere WI
- .NET Tip: Execute Commands From C# Delavan WI
- .NET Tip: Execute Commands From C# Eau Claire WI
- .NET Tip: Execute Commands From C# Elkhorn WI
- .NET Tip: Execute Commands From C# Fond Du Lac WI
- .NET Tip: Execute Commands From C# Fort Atkinson WI
- .NET Tip: Execute Commands From C# Franklin WI
- .NET Tip: Execute Commands From C# Green Bay WI
- .NET Tip: Execute Commands From C# Hartland WI
- .NET Tip: Execute Commands From C# Janesville WI
- .NET Tip: Execute Commands From C# Kaukauna WI
- .NET Tip: Execute Commands From C# Kenosha WI
- .NET Tip: Execute Commands From C# La Crosse WI
- .NET Tip: Execute Commands From C# Lake Geneva WI
- .NET Tip: Execute Commands From C# Manitowoc WI
- .NET Tip: Execute Commands From C# Marinette WI
- .NET Tip: Execute Commands From C# Marshfield WI
- .NET Tip: Execute Commands From C# Menasha WI
- .NET Tip: Execute Commands From C# Menomonee Falls WI
- .NET Tip: Execute Commands From C# Menomonie WI
- .NET Tip: Execute Commands From C# Merrill WI
- .NET Tip: Execute Commands From C# Middleton WI
- .NET Tip: Execute Commands From C# Milwaukee WI
- .NET Tip: Execute Commands From C# Mosinee WI
- .NET Tip: Execute Commands From C# Mukwonago WI
- .NET Tip: Execute Commands From C# Muskego WI
- .NET Tip: Execute Commands From C# Neenah WI
- .NET Tip: Execute Commands From C# New Berlin WI
- .NET Tip: Execute Commands From C# Oak Creek WI
- .NET Tip: Execute Commands From C# Oconomowoc WI
- .NET Tip: Execute Commands From C# Onalaska WI
- .NET Tip: Execute Commands From C# Oshkosh WI
- .NET Tip: Execute Commands From C# Pewaukee WI
- .NET Tip: Execute Commands From C# Racine WI
- .NET Tip: Execute Commands From C# Rhinelander WI
- .NET Tip: Execute Commands From C# Rice Lake WI
- .NET Tip: Execute Commands From C# River Falls WI
- .NET Tip: Execute Commands From C# Schofield WI
- .NET Tip: Execute Commands From C# Shawano WI
- .NET Tip: Execute Commands From C# Sheboygan WI
- .NET Tip: Execute Commands From C# South Milwaukee WI
- .NET Tip: Execute Commands From C# Stevens Point WI
- .NET Tip: Execute Commands From C# Sturgeon Bay WI
- .NET Tip: Execute Commands From C# Sun Prairie WI
- .NET Tip: Execute Commands From C# Superior WI
- .NET Tip: Execute Commands From C# Thiensville WI
- .NET Tip: Execute Commands From C# Two Rivers WI
- .NET Tip: Execute Commands From C# Watertown WI
- .NET Tip: Execute Commands From C# Waukesha WI
- .NET Tip: Execute Commands From C# Waupaca WI
- .NET Tip: Execute Commands From C# Wausau WI
- .NET Tip: Execute Commands From C# West Bend WI
- .NET Tip: Execute Commands From C# Whitewater WI
- .NET Tip: Execute Commands From C# Wisconsin Rapids WI
Related Articles
- Programming MIDlets for Interactive Behavior Wisconsin
Discover how to add commands to a MIDlet and how those commands are presented to the user in the Sun cell phone emulator. You will also learn how to produce interactive behavior within a MIDlet.
- HipCheck Administrative Command Wisconsin
- Working With Design Patterns: Memento Wisconsin
- Working with Design Patterns: Command Wisconsin
- Drilling Into SQL Server Management Studio Wisconsin
- Secrets of the MySQL Client Command Line Wisconsin
Related Local Events
2008 Early Stage Symposium
Dates: 11/5/2008 - 11/6/2008
Location: Monona Terrace
Madison WI
View Details

Wisconsin Entrepreneurs' Conference
Dates: 6/9/2008 - 6/10/2008
Location: Hyatt Regency Hotel
Milwaukee WI
View Details
Rate Article
     
Articles Insider

Rss   Delicious   Digg   Add To My Yahoo   Add To My Google   Bookmark   Search Plugin

Topics:
Advertising Engineering Home Services Software
Business Services Entertainment Industrial Goods & Services Technology
Career Family Insurance Telecommunications
Cars Financial Services Internet Transportation & Logistics
Computer Hardware Food & Beverage Legal Travel
Construction Health Real Estate Wedding
Education Home Electronics Retail & Consumer Services