In this article i am going to demonstrate how we can execute the underlying OS commands in ASP.NET. First of all you need to change the IIS configuration settings. Go to control Panel - > Administrative tools -> Services -> Select ISS Admin Properties -> Goto logon tab and check the "Allow service to interact with desktop" . The following code demonstrates the compiling of a C file named 'abc'. First of all you have to use the name space using System.Diagnostics.
Process mpr = new Process();
mpr.StartInfo.FileName = "Cmd.exe";
mpr.StartInfo.Arguments = @"/C tcc c:\abc.c";
mpr.StartInfo.RedirectStandardError = true;
mpr.StartInfo.RedirectStandardOutput = true;
mpr.StartInfo.UseShellExecute = false;
Now lets have a look at the code First of all you Create an instance of Process class. Then you specify the application to be executed. In the third line you specify any arguments that is to be passed to the application. Now comes the redirection part. What does the 4th and 5th line means.When a Process is executed it typically writes its Errors and outputs to its "standard error/outout stream". By using 'RedirectStandardError/RedirectStandardOutput' we can can manipulate or suppress the error/output of a process. i.e. we can filter,modify or even write the output to both the console and a designated log file.
We were talking about executing shell commands and here is the line that says
"mpr.StartInfo.UseShellExecute = false;"
what does it mean?.
By default this property is true. And when this property is set to true we can perform any operation using Process component like starting a document,perform operations on files like printing etc.. But the problem is that we can't redirect the input, output, and error streams. This property must be set to false to redirect all the above. So what happens when we set this property to false? When set to false the process is created directly from the executable file. i.e. simply saying you can start only executables with the Process component.
The next line 'mpr.StartInfo.CreateNoWindow= true;' starts the process without creating a new window to contain it. And finally 'mpr.Start();' starts your process.
I hope it was informative and use ful to you. If you find any errors (technical) please inform us.
NB: In the near future we are planning to migrate this blog to a website. Hope all of you will support us.