Process has been behaving oddly and I finally narrowed the symptoms down, but even the documentation has been unhelpful in finding a solution. The class works fine when I don't specify Domain, UserName, and Password -- I can pass Arguments with or without quotes.
But when I specify Domain, UserName, and Password -- it works as long as there are no quoted Arguments, but if any Arguments are quoted the Process doesn't Start (and doesn't seem to fail or throw an Exception or anything).
I have read through the Process and ProcessStartInfo documentation and tried tweaking some properties, but to no avail.
To test and demonstrate the problem I wrote a simple BAT file:
echo %time% > C:\temp\test.out
echo %* >> C:\temp\test.out
And a simple console application to execute it via Process:
namespace Test
{
public static class Test
{
public static void
Main
(
string[] args
)
{
System.IO.File.Delete ( @"C:\temp\test.out" ) ;
System.Diagnostics.Process pr = new System.Diagnostics.Process() ;
pr.StartInfo.FileName = @"C:\bin\test.bat" ;
if ( args.Length % 2 == 0 )
{
pr.StartInfo.Arguments = @"one two three four" ;
}
else
{
pr.StartInfo.Arguments = @"one ""two three"" four" ;
}
System.Console.WriteLine ( pr.StartInfo.Arguments ) ;
pr.StartInfo.UseShellExecute = false ;
if ( args.Length > 2 )
{
pr.StartInfo.Domain = args [ 0 ] ;
pr.StartInfo.UserName = args [ 1 ] ;
System.Security.SecureString pwd = new System.Security.SecureString() ;
foreach ( char c in args [ 2 ] )
{
pwd.AppendChar ( c ) ;
}
pwd.MakeReadOnly() ;
pr.StartInfo.Password = pwd ;
}
pr.Start() ;
}
}
}
So, if you specify Domain, UserName, and Password as parameters it will try to run as that user. If the number of parameters is odd, it will put quotes around a parameter.
After running the app you can examine the contents of the output file (if it exists).
I don't know where else to seek a solution. Has anyone out there run into this and found a solution?
Update: Using CMD.EXE as the Filename and passing /C and the name of the bat file in Arguments seems to work as well, so I'll go with that for now.