0
我正在編寫批處理文件並通過c#程序執行它。從C#程序執行BatchFile
編寫批處理文件:
我會得到路徑,可執行文件名和參數從app.config中,並將其寫入到一個批處理文件。
執行批處理文件:
一旦我寫批處理文件我通過文件名下面的功能,其執行批處理文件啓動一個應用程序。
問題:
我的程序會寫很多,它們分別後立即執行,每個文件被寫入批處理文件。我發現,有些時候應用程序沒有啓動,這意味着批處理文件沒有執行。我甚至得到任何錯誤消息或提示批處理文件執行失敗。
預期溶液:
在執行批處理文件中的任何問題,我應該能夠登錄,或提示錯誤。執行批處理文件
代碼:
System.Diagnostics.ProcessStartInfo procinfo = new System.Diagnostics.ProcessStartInfo("cmd.exe");
procinfo.UseShellExecute = false;
procinfo.RedirectStandardError = true;
procinfo.RedirectStandardInput = true;
procinfo.RedirectStandardOutput = true;
System.Diagnostics.Process process = System.Diagnostics.Process.Start(procinfo);
System.IO.StreamReader stream = System.IO.File.OpenText(BatchPath + LatestFileName);
System.IO.StreamReader sroutput = process.StandardOutput;
System.IO.StreamWriter srinput = process.StandardInput;
while (stream.Peek() != -1)
{
srinput.WriteLine(stream.ReadLine());
}
Log.Flow_writeToLogFile("Executed .Bat file : " + LatestFileName);
stream.Close();
process.Close();
srinput.Close();
sroutput.Close();
非常urgent..Help!
我創建一個.Bat文件並在cmd.exe中執行。在這種情況下,XYZ.cmd是哪裏?我在這裏錯過了什麼嗎?一旦我寫了一個批處理文件,我把它命名爲「string.Format(」{0:dd-MM-yyyy [HH-mm-ss-fffff]}「,dt);」使每個批處理文件名稱唯一。幫助...謝謝。 – Anuya 2010-09-23 02:51:52
@Anuya,你正在運行'cmd.exe'並將批處理文件的每一行傳遞給它的標準輸入。這似乎是一個複雜的方式來做到這一點。由於批處理文件已經在磁盤上,我只需運行它而不是'cmd.exe',那麼您不必擔心將單個行發送給它。只需將我的給定代碼中的'xyz.cmd'替換爲您自己的批處理文件名(基於日期/時間)即可。我的'使用'位就是爲了向你展示我是如何創建批處理文件的。實際運行以'Process p = ...'位開始。 – paxdiablo 2010-09-23 03:09:59