Tuesday, May 27, 2008

StandardOutput property for Process (Shell programming in C#.Net)



Most of u must be working on Shell programming in C#.Net...
I am also working on it.. Hence thought to share some useful things with you...

Importance of RedirectStandardError , RedirectStandardOutput properties..

Generally, we used to start programming like this..
The following code example is for running Al.exe to generate dll from resource file..

proc_new = new Process();
proc_new.StartInfo.UseShellExecute = false;
proc_new.StartInfo.ErrorDialog = true;
proc_new .StartInfo.Arguments = "/embed:" + ResFile.resources+ " /out:" + New.dll
proc_new .StartInfo.FileName = "//" + Al.exe
proc_new
.Start();

The above works fine, if Al.exe and its dependencies (like, alnk.dll and alinkui.dll are present).
These dependencies are present on your machine only is Visual studio installed. If not, you will have to manually copy it to the Same directory where Al.exe is residing.

Generally we don't think on such issues.
We just think that, I have installed .Net Framework on PC, where application is to be tested.Also, from C# Code we don't get any exception "Some dependency dll is missing )..

To ensure all things to be done in proper way ..
use following prop. for process as follows :

string stroutput = proc_new.StandardOutput.ReadToEnd().ToString();
string strError= proc_new.StandardError.ReadToEnd().ToString();

If some dependency is missing, we don't get standard output. stroutput will have blank value...
If process generated dll, it will give standard output same as we get when we run command from dos shell.

In case, if some dll is missing , sometimes we dont get error in strError also.. So its better to use strOutput and cross check it with standard output....

P.N. I have experienced this problem with Framework 1.0 and 2.0, I have no idea of versions 3.0 and later...


Hope , this information will find useful for you all...


Jicky