0

I'm trying to do execute an external .bat by pressing a button.

The intention is to call some XCOPY instructions. Therefore I execute "sync.bat" using Process.Start(startInfo).

The output of that .bat is redirected to my App and shown in a dialog box. My code waits until the external call has finished.

echo "Batch SYNC started."
pause
xcopy "e:\a\*" "e:\b\" /f /i /c /e /y
pause
echo "Batch SYNC finished."

OK: When I build my program as "release" and start it within VisualStudio2013, everything works fine (I see the Results, have to press ENTER in the black window, files are copied).

FAIL: When I start my app by double-click (in file-explorer or from the desktop) or a debug build within the VisualStudio, I see the ECHO and the PAUSE output, but the batch did not stop and I see no results from XCOPY. Seems as if the PAUSE and XCOPY are killed immediately. I got no exception and no entry in Windows-log.

I have tried to make DEBUG and RELEASE configuration identical (with no success).

Does anybody have an idea what I may do to get this simple function work outside the IDE?

Here is the code of the function called when the button is pressed:

private void ProcessSync_bat()
{
        ProcessStartInfo startInfo = new ProcessStartInfo();
        startInfo.CreateNoWindow = false;
        startInfo.UseShellExecute = false;
        startInfo.RedirectStandardOutput = true;
        startInfo.RedirectStandardError = true;
        startInfo.FileName = "sync.bat";
        startInfo.WindowStyle = ProcessWindowStyle.Normal;
        startInfo.Arguments = "";
        startInfo.ErrorDialog = true;

        try
        {
            // Start the process with the info we specified.
            // Call WaitForExit and then the using statement will close.
            using (Process exeProcess = Process.Start(startInfo))
            {
                dlgFKSyncMessageBox.AddLine("----------sync.bat started-----------");
                dlgSyncMessageBox.AddLine("===============Result================");
                while (!exeProcess.StandardOutput.EndOfStream)
                {
                    dlgSyncMessageBox.AddLine(exeProcess.StandardOutput.ReadLine());
                }
                dlgSyncMessageBox.AddLine("===============ERRORS================");
                while (!exeProcess.StandardError.EndOfStream)
                {
                    dlgSyncMessageBox.AddLine(exeProcess.StandardError.ReadLine());
                }
                exeProcess.WaitForExit();
            }
        }
        catch (Exception exp)
        {
            dlgSyncMessageBox.AddLine("========EXCEPTION========");
        }
}
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Dietmar
  • 1
  • 2
  • 1
    Is your bat file copied the the respective output folder? I'd suspect you only copied it to `bin\release` so it will not be found when running from `bin\debug`. – Filburt Oct 16 '17 at 07:40
  • Did you check tha answer here: https://stackoverflow.com/questions/21731783/xcopy-or-move-do-not-work-when-a-wcf-service-runs-a-batch-file-why You may need to remove `RedirectStandardOutput` options – Bassie Oct 16 '17 at 07:48

1 Answers1

0

Solution: If I additionally set

startInfo.RedirectStandardInput = true;

then it works. I may redirect input from dialog window to the Process. Since I do not need any input for the intended XCOPY, this solution works for me without catching chars from the Dialog window and Forward to the process. I can't see the logic, why I have to Redirect input too, but I'm glad that my Software now does what I need.

Dietmar
  • 1
  • 2