Click here to Skip to main content
6,634,665 members and growing! (15,703 online)
Email Password   helpLost your password?
Languages » C# » Samples     Intermediate

Run other programs from your .NET code

By Xiangyang Liu 刘向阳

A .NET library and a sample application demonstrating simple use of the Process class.
C#, VB.NET 1.0, .NET 1.1, .NET 2.0, Win2K, WinXP, Win2003, ASP.NET, VS.NET2003, VS2005, Dev
Posted:26 Aug 2004
Views:152,740
Bookmarked:84 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
31 votes for this article.
Popularity: 6.49 Rating: 4.35 out of 5
2 votes, 6.5%
1
1 vote, 3.2%
2
2 votes, 6.5%
3
6 votes, 19.4%
4
20 votes, 64.5%
5

Introduction

Once upon a time, a CP member paid $19.95 per month for web hosting through a professional hosting provider and registered his own domain name. He had no practical use for it, he did this just because everyone else seemed to have done so and he did not want to miss the fun. The provider used Windows XP servers to host .NET web applications.

The said CP member was learning the Process class in the .NET framework at the time. He found out that he could upload almost any application and execute it on the server machine using this class. The ASPNET account on the server machine had only limited access rights, but he could have done a lot of things on the server that he should not be allowed to do.

Anyway, he lost interest later and cancelled the account with the provider.

A .NET library to execute other programs

I have included a .NET DLL with this article that demonstrates the use of the Process class in the .NET framework. This DLL contains one class called CommandObj with a single public method Run to start another application. You can use multiple instances of CommandObj to start multiple applications from your code. The applications being started do not have to be .NET applications, however.

The help documentation on the Process class is so clear that I feel no need to explain any detail here. What I did in the CommandObj class is make it easier for users to run another program from their code. All you have to do is create an instance of CommandObj and call the Run method. Here is the signature of the Run method.

public string Run(string sFilePath, string sArgs, 
    string sInput, string sWorkingDir, int nWaitTime);
  • The sFilePath parameter is the full path of the program you want to run.
  • The sArgs parameter is the optional command line arguments, use null or empty string if there are no arguments.
  • The sInput parameter is a string that will be written to the standard input stream of the program you want to run, use null or empty string if your program does not expect any input.
  • The sWorkingDir is the working directory for the program you want to run. If you pass null or the empty string, the working directory will be inherited from the parent process.
  • Finally, the nWaitTime parameter is the number of milliseconds the current CommandObj object will wait for the program it started. If you pass 0 or a negative value, the internal default value 60000 (one minute) will be used.

The return value of the Run method is whatever the program wrote to its standard output and/or standard error streams. Note that the Run method will not return until the application it started has terminated. But you can modify the CommandObj class to do things differently or call the Run method from a separate thread so that your main thread won't be blocked.

Here is what will happen when you call the Run method. First, a ProcessStartInfo object (see .NET documentation) will be created with the given file path and command line. Then the working directory will be set, the standard output and standard error streams will be redirected. Finally, the program you specified will be started by a Process object and its output and error will be read from separate threads. If the started program does not end within the given time, it will be forcefully terminated; when that happens, the return value of the Run method will contain the string "Error: Hung process terminated ...".

Various exceptions while calling the Run method should be handled by the caller. Typical examples of exceptions include wrong file path, access violation, authentication error, etc.

The use of the CommandObj class is not limited to web applications, there is nothing to prevent you from using it to run a GUI program.

RemoteAdmin, a sample ASP.NET application

If you are working with multiple servers or workstations like me, it can be annoying that you have to logon to different machines all the time. It would be nice if you can logon to one machine and use the browser to access other machines.

RemoteAdmin is an ASP.NET application built for this purpose. If you install it on a remote machine, you can do all kinds of things without logon to the remote machine. I typically use it to view logs, upload files, replace a binary module (a .NET DLL, for example), mount a network drive, or make configuration changes for a web application (replace the web.config file).

There is only one web form in this ASP.NET application. The picture on the left shows how this form is displayed and used in the browser. There is a "submit" button on the lower right corner of the web form. When this button is clicked, the cmd.exe program will be executed on the server from the directory d:\ and the content of the directory will be displayed on the same page below the web form (see the lower portion of the picture). This is equivalent to logon to the server, open a command prompt window, and executing the following commands:

d:
dir

As you can see, you can place a whole bunch of DOS commands into the "Standard Input" field on the form and execute them with a single click of a button. Please note that you need to end DOS commands with a return character.

If the remote machine runs .NET applications under the "local system" account, then RemoteAdmin will have administrator privilege (and more), you can even install, start, and stop Windows services. For security risks associated with this, please read the next section.

To install RemoteAdmin, do the following: unzip all files into a local folder; run the script file InstallRemoteAdmin.vbs. The script will ask you on which website (if there is more than one) you want to run this application. It will also ask you what IP address is allowed to access this application. You can modify the script to allow access from more than one IP address. Unless you are working in a secured environment with trustworthy co-workers, you don't want to let everyone access your machine without limitation.

After installation, you access the application from the specified IP address by typing the following URL into the address bar of your browser:

http://MyRemoteMachine/RemoteWebAdmin/RemoteAdmin.aspx?Action=Run

If you change Action=Run in the query string to Action=Load, then the form will change its appearance to allow you to upload any file to the remote machine.

Security Concerns And Disclaimer

As I said or implied earlier, it is easy for others to use the RemoteAdmin application to compromise your server. That is why you are not supposed to install it on critical servers. On the machines you do install it, you are recommended to restrict access to a few trusted IP addresses. Since IP addresses can be faked, you may want to implement further authentication/authorization mechanism. You should never modify the machine.config file on your server to let ASP.NET applications run under the "local system" account; even if other people can't access your machine, you can easily do a lot of damages yourself.

In any case, I will not be responsible for any problem resulting from using the code and ideas in this article.

Thank you for reading my article.

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

Xiangyang Liu 刘向阳


Member

Location: United States United States

Other popular C# articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 41 (Total in Forum: 41) (Refresh)FirstPrevNext
Generalgreat Pinmemberernest_elias7:14 13 Mar '09  
GeneralErrors whenever Path names in Arguments [modified] PinmemberMember 31216945:55 15 Aug '08  
GeneralI WANT THE SOURCE Pinmemberakdenizl23:21 11 Aug '08  
GeneralRe: I WANT THE SOURCE PinmemberXiangyang Liu ???4:46 12 Aug '08  
GeneralGood that you are sharing this code but this is not your code!!!! I wrote this should be removed Pinmemberjkim111412:52 30 May '08  
GeneralRe: Good that you are sharing this code but this is not your code!!!! I wrote this should be removed PinmemberXiangyang Liu15:19 30 May '08  
Generalhow can we cal arguments in the exe Pinmemberbeklo.com/rameshgoudd18:53 11 Oct '07  
GeneralPing command is not working with your code? Pinmembertry.test.abc19:46 16 Feb '07  
GeneralTried it but no results PinmemberKoen Notebaert0:28 5 Jan '07  
QuestionRunning on IIS Pinmemberrafikki2:23 24 Aug '06  
GeneralA .NET library to execute other programs Pinmemberlarsp7778:37 10 Aug '06  
GeneralBloody Sucker Pinmemberjustsuraj1:28 7 Aug '06  
GeneralHow to .exe applications from c# .net Pinmemberchinmmay19:56 5 Apr '06  
Generalprompts Pinmembershamsallana23:28 28 Feb '06  
GeneralUsing Arguments PinmemberRoy Oliver8:45 1 Feb '06  
GeneralRe: Using Arguments PinmemberXiangyang Liu11:16 1 Feb '06  
GeneralRe: Using Arguments PinmemberRoy Oliver19:33 1 Feb '06  
GeneralRe: Using Arguments PinmemberRoy Oliver7:52 4 Feb '06  
GeneralRe: Using Arguments PinmemberRoy Oliver15:14 6 Feb '06  
GeneralRe: Using Arguments Pinmembergokul.sg@gmail.com9:52 24 May '06  
GeneralClose main application PinsussCarlos Arturo Gómez Teshima6:55 14 Mar '05  
GeneralUser impersonation? Pinmemberdzarn11:46 21 Dec '04  
Generalfrom a web service Pinmemberizz23:24 21 Nov '04  
GeneralRe: from a web service Pinmemberizz23:31 21 Nov '04  
GeneralRe: from a web service PinmemberXiangyang Liu5:32 22 Nov '04  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 26 Aug 2004
Editor: Smitha Vijayan
Copyright 2004 by Xiangyang Liu 刘向阳
Everything else Copyright © CodeProject, 1999-2009
Web18 | Advertise on the Code Project