Click here to Skip to main content
Licence 
First Posted 26 Aug 2004
Views 428,402
Bookmarked 106 times

Run other programs from your .NET code

By | 26 Aug 2004 | Article
A .NET library and a sample application demonstrating simple use of the Process class.

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 刘向阳



United States United States

Member



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. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralError when I modified this to run as a specific user Pinmemberjbc16:36 5 May '11  
GeneralUsing this a bit differently [modified] PinmemberPaola Badillo10:04 9 Jul '10  
Generalgreat Pinmemberernest_elias6:14 13 Mar '09  
GeneralErrors whenever Path names in Arguments [modified] PinmemberMember 31216944:55 15 Aug '08  
GeneralI WANT THE SOURCE Pinmemberakdenizl22:21 11 Aug '08  
GeneralRe: I WANT THE SOURCE PinmemberXiangyang Liu ???3:46 12 Aug '08  
GeneralGood that you are sharing this code but this is not your code!!!! I wrote this should be removed Pinmemberjkim111411: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 Liu14:19 30 May '08  
jkim1114 wrote:
Hey,
This is not your code this should be remove!!!!!

 
What makes you think it is not my code? In fact it is, every line of it. Did you see it on some other website? Did you write it yourself? Please explain.
 
If you did see it somewhere else, please give me a link. A lot of my code has been republished in other places with my name missing, someone even copied it in a book without given me credit. I would appreciate you gave more detail on where you saw it. Thanks.
 

Questionhow can we cal arguments in the exe Pinmemberbeklo.com/rameshgoudd17:53 11 Oct '07  
QuestionPing command is not working with your code? Pinmembertry.test.abc18:46 16 Feb '07  
GeneralTried it but no results PinmemberKoen Notebaert23:28 4 Jan '07  
GeneralRe: Tried it but no results Pinmemberbellay20:02 15 Aug '11  
QuestionRunning on IIS Pinmemberrafikki1:23 24 Aug '06  
GeneralA .NET library to execute other programs Pinmemberlarsp7777:37 10 Aug '06  
GeneralBloody Sucker Pinmemberjustsuraj0:28 7 Aug '06  
QuestionHow to .exe applications from c# .net Pinmemberchinmmay18:56 5 Apr '06  
Generalprompts Pinmembershamsallana22:28 28 Feb '06  
GeneralUsing Arguments PinmemberRoy Oliver7:45 1 Feb '06  
GeneralRe: Using Arguments PinmemberXiangyang Liu10:16 1 Feb '06  
GeneralRe: Using Arguments PinmemberRoy Oliver18:33 1 Feb '06  
GeneralRe: Using Arguments PinmemberRoy Oliver6:52 4 Feb '06  
GeneralRe: Using Arguments PinmemberRoy Oliver14:14 6 Feb '06  
GeneralRe: Using Arguments Pinmembergokul.sg@gmail.com8:52 24 May '06  
GeneralClose main application PinsussCarlos Arturo Gómez Teshima5:55 14 Mar '05  
QuestionUser impersonation? Pinmemberdzarn10:46 21 Dec '04  

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120529.1 | Last Updated 27 Aug 2004
Article Copyright 2004 by Xiangyang Liu 刘向阳
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid