Click here to Skip to main content
15,888,803 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.
Posted
Updated 23-Nov-13 5:55am
v2

1 solution

Have a look at the Sysinternals toolbox[^] - it's free and it's micro$oft.

Specifically have a look at the ProcMon tool - you can monitor what processes are doing (e.g. what happens when they access a file, what the command line is, what dll's they load etc etc) - hopefully that will give you the information you need to find out what's going on.

I use this tool daily and would be lost without it
 
Share this answer
 
Comments
PIEBALDconsult 23-Nov-13 11:41am    
I'll take a look, but if the process doesn't start I don't think it can help.
CHill60 23-Nov-13 12:45pm    
If you monitor your app that is trying to start the process you may get some clues ... I recently thought that an app at our place wasn't loading and used Procmon to discover that it was starting but aborting immediately, so I'd been looking in the wrong place
Nelek 23-Nov-13 11:42am    
Tool seems nice. Thanks for the link

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900