0

I am developing Windows Service which will generate csv file through batch file.Later I will read the file. Below is the batch file command psexec \wksp000ae2c8 -h -w D:\UPSData\OCCG\OPMI\Cache\OPSTimingExtractor\ "C:\Program Files (x86)\UPSAPPS\OCCG\OPMI\Tools\dirt.bat" > 051120072244.091.csv

I am able to generate to the csv file when I execute in Console individually. The Windows Service generates batch file.

But when I execute it as windows Service Process.WaitForExit does not return.

I individually double click the batch file it executes and generates the batch file . Below is the code

 Process procstart = new Process();
            ProcessStartInfo psi = new ProcessStartInfo
            {
                CreateNoWindow = true,
                UseShellExecute = false,
                FileName = "cmd.exe",
                Arguments = @"/C "+ BatfileName,
                WindowStyle = ProcessWindowStyle.Hidden,
                RedirectStandardOutput = true,
                RedirectStandardError = true,
                ErrorDialog = false
            };

            procstart.StartInfo = psi;
            procstart.Start();
            procstart.WaitForExit();

When I add procstart.Kill or Close it does not work. When I go to location and try to open the file it says FIle is in another process.

When I try to delete the file it says "The action cannot be completed because the file is in open in cmd.exe

sachin kulkarni
  • 2,618
  • 7
  • 28
  • 33
  • could it be waiting for a keystroke? contents of dirt.bat? – Cee McSharpface Nov 05 '20 at 13:35
  • I am using Windows Service.Not sure how keystroke would come into picture.Also When I double click there is no keystroke. – sachin kulkarni Nov 05 '20 at 13:36
  • because if a batch file expects a keystroke before exit, it would do just what you observe when called from a service process. only a suspicion though. – Cee McSharpface Nov 05 '20 at 13:40
  • possibly related: https://stackoverflow.com/a/31945741/1132334, furthermore https://serverfault.com/a/437508/387902 ("*PsExec, it just never returns until I hit the "enter" key on my CMD prompt* when called from a service process) – Cee McSharpface Nov 05 '20 at 13:42
  • I just checked Batch job individually running.generating csv without keystroke – sachin kulkarni Nov 05 '20 at 13:49
  • When I change the location of Batch file then It is not generating csv file also.When I copy Bat file and double click it it is not producing the csv file – sachin kulkarni Nov 05 '20 at 14:58

1 Answers1

0

I achieved this by adding exit command in batch file.Also I had to executed the file with my credentials.

sachin kulkarni
  • 2,618
  • 7
  • 28
  • 33