4

I have a bat file that copies files from one location to another.

SET SRC=%1
SET DEST=%2

xcopy /Y/I %SRC%\*.txt %DEST%\temp
echo Done!

I'm trying to run this file via C# program

var psi = new ProcessStartInfo(fileToRun);
psi.Arguments = args;
psi.RedirectStandardOutput = true;
psi.RedirectStandardError = true;
psi.WindowStyle = ProcessWindowStyle.Hidden;
psi.UseShellExecute = false;
psi.CreateNoWindow = true;

Process cmdProc = Process.Start(psi); 

StreamReader output = cmdProc.StandardOutput;
StreamReader errors = cmdProc.StandardError;
cmdProc.WaitForExit();

Bat-file is executed, I can see the 'Done!' message in the output, but files are not copied.

The only way it works is

psi.UseShellExecute = true;

psi.RedirectStandardOutput = false;
psi.RedirectStandardError = false;

But in this case I have to disable output/error redirection and I need them. So this doesn't work for me.

I have tried to set administrator's username/password

psi.UserName = username;
psi.Password = password; 

Logon succeed, but I get the 'The handle is invalid' message in the StandardError Stream.

I guess the process I'm trying to run doesn't have permissions to copy files and I don't know how to grant him these permissions.

Please, help!

EDITED

Thank you for replies! I have spend several hours trying to handle this issue and as it always happens I have posted my question and found the solution :)

In order to avoid getting 'The handle is invalid' message you have to

psi.RedirectStandardInput = true;

But now I can see cmd.exe window, if UserName is set, which is bad.

Igor
  • 131
  • 1
  • 9
  • In case where you capture the output of `XCOPY` what does it say? It should include `Access denied` (or your language's equivalent) if permissions are the issue. – Christian.K Jan 21 '12 at 09:47
  • It said nothing, failed silently, no errors in standard error stream. – Igor Jan 21 '12 at 09:57
  • Temporarly put an `ECHO` infront of the `XCOPY`. Does the ouptut show the command line (source/target directories, etc.) you expect? – Christian.K Jan 21 '12 at 10:27
  • Also, what exactly is in `args` and `fileToRun` when the failure happens? – Christian.K Jan 21 '12 at 10:31
  • Yes, it does. I have found the solution, but now I have another problem - I can see cmd.exe window even though psi.WindowStyle = ProcessWindowStyle.Hidden; psi.CreateNoWindow = true; – Igor Jan 21 '12 at 10:33
  • Similar question with same solution and more info: http://stackoverflow.com/questions/1910592/process-waitforexit-on-console-vs-windows-forms – joerage Sep 20 '12 at 03:04
  • to understand : you want to start a batch file with a specific user account , while the batch file runs it shouldn't ask / wait for username and whatever supplied via c# just use that , correct ? – Baljeetsingh Sucharia Mar 08 '14 at 02:55

1 Answers1

1

you are missing

psi.Domain = "domain";
psi.Verb ="runas";
//if you are using local user account then you need supply your machine name for domain

try this simple snippet should work for you

void Main()
{
    string batchFilePathName =@"drive:\folder\filename.bat";
    ProcessStartInfo psi = new ProcessStartInfo(batchFilePathName);

    psi.Arguments = "arg1 arg2";//if any
    psi.WindowStyle = ProcessWindowStyle.Hidden;
    psi.UseShellExecute = false;
    psi.Verb ="runas";
    psi.UserName = "UserName"; //domain\username
    psi.Domain = "domain"; //domain\username
    //if you are using local user account then you need supply your machine name for domain

    psi.WindowStyle = ProcessWindowStyle.Hidden;
    psi.UseShellExecute = false;
    psi.Verb ="runas";

    Process ps = new Process(psi);
    Process.Start(ps);  
}
Baljeetsingh Sucharia
  • 1,990
  • 1
  • 19
  • 37
  • this might come handy if you run across any windows UAC issues http://stackoverflow.com/questions/2818179/how-to-force-my-net-app-to-run-as-administrator-on-windows-7 – Baljeetsingh Sucharia Mar 08 '14 at 03:22