Chai  Rao

Chai Rao

  • NA
  • 5
  • 3k

Process class to start bat files which uses fileshares

Apr 14 2010 1:53 PM
I have a C# program (which runs either as a service or a windows app) that starts either .exe's or .bat files on a Windows machine - it uses the PROCESS class to do this. I also capture standard output from it - all of this works to my satisfaction.

When I run the bat file (it transfers a file from a share to the local d-drive), as a logged on user by simply double-clicking on it via Windows Explorer, it works fine. However, if I start this same bat file from my program, the bat file does not do what it is supposed to do. The bat file does run but is not successful in transferring the file.

I have looked at all possible attributes for StartInfo with no success. I have had success by changing the bat file to include a "NET USE " command to map the drive - but that requires changing the bat files which I may not always have the luxury to do. I want the bat file to run as is.

The bat file contents are:

cd C:\
copy s:\eftsource\cards041210 d:\eftrans\outgoing

The lines of code from my C# program are:

     // Current Process
     currentProcess = new Process();
     currentProcess.StartInfo.FileName = strJobFilename;
     currentProcess.StartInfo.Arguments = strJobArguments;
     currentProcess.StartInfo.WorkingDirectory =
     Path.GetDirectoryName(strJobFilename);
     strExtension = Path.GetExtension(strJobFilename);
     currentProcess.StartInfo.

     currentProcess.StartInfo.UseShellExecute = false;
     // Redirect Standard Output
     currentProcess.StartInfo.RedirectStandardOutput = true;
     currentProcess.OutputDataReceived +=
                   new DataReceivedEventHandler(StandardOutputHandler);
     // Redirect Standard Error
     currentProcess.StartInfo.RedirectStandardError = true;
     currentProcess.ErrorDataReceived +=
                   new DataReceivedEventHandler(StandardErrorHandler);
     // Other StartInfo properties
     currentProcess.StartInfo.CreateNoWindow = true;

     try
     {
         dtBeforeStartTime = DateTime.Now;
         bProcessStarted = currentProcess.Start();
     }

Regards
Chai.