我試圖通過按按鈕來執行外部.bat。Process.Start()僅適用於在VisualStudio中以發佈版本的形式啓動時
其意圖是調用一些XCOPY指令。因此我使用Process.Start(startInfo)執行「sync.bat」。
該.bat的輸出被重定向到我的應用程序並顯示在對話框中。我的代碼一直等到外部通話結束。
echo "Batch SYNC started."
pause
xcopy "e:\a\*" "e:\b\" /f /i /c /e /y
pause
echo "Batch SYNC finished."
OK: 當我建立我的節目爲「釋放」和內VisualStudio2013啓動它,一切工作正常(我看到的結果,必須按ENTER鍵在黑色的窗口,文件複製)。
失敗: 當我通過雙擊(在文件資源管理器或桌面)或VisualStudio的內部調試版本開始我的應用程序,我看到了ECHO和暫停輸出,但批處理沒有停下來,我看不到XCOPY的結果。看起來好像PAUSE和XCOPY立即被殺死。 我沒有例外,並沒有在Windows日誌中的條目。
我試圖使DEBUG和RELEASE配置相同(沒有成功)。
有沒有人有一個想法,我可以做些什麼來讓這個簡單的功能在IDE之外工作?
這裏是按下按鈕時調用的函數的代碼:
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========");
}
}
你的bat文件是否複製了相應的輸出文件夾?我懷疑你只是將它複製到'bin \ release',所以從'bin \ debug'運行時不會找到它。 – Filburt
你檢查了答案在這裏:https://stackoverflow.com/questions/21731783/xcopy-or-move-do-not-work-when-a-wcf-service-runs-a-batch-file-why你可能需要刪除'RedirectStandardOutput'選項 – Bassie