Issue when running a Process under a user's context - CRITICAL

Feb 8 2007 12:39 AM
 

Hi,

 I am trying to execute a dos command under a different user. Following is the code..

// We create a process, assign its ProcessStartInfo and start it

System.Diagnostics.Process proc = new System.Diagnostics.Process();

proc.StartInfo.UserName = "UserName";

proc.StartInfo.Password = passwd;

proc.StartInfo.Domain = "DOMAIN";

proc.StartInfo.UseShellExecute = false;

proc.StartInfo.FileName = "cmd";

proc.StartInfo.Arguments = "/c dir";

proc.StartInfo.RedirectStandardOutput = true;

proc.StartInfo.CreateNoWindow = true ;

proc.Start();

// Get the output into a string

result = proc.StandardOutput.ReadToEnd();

 The last line gives me the output of the command execution. When I try executing a command under the default login i.e. without providing any username, password and domain name, the command gets executed properly. But when I try to execute this command under some user's context, it throws an exception System.ComponentModel.Win32Exception  --> "The handle is invalid.". Also, this exception is raised when we include the following line of code :

proc.StartInfo.RedirectStandardOutput = true;

Can somebody help me in resolving this problem ?

Thanks a lot !

-Sandeep