从C#程序执行批处理文件
我正在编写批处理文件并通过C#程序执行它 写入批处理文件:从C#程序执行批处理文件,c#,C#,我正在编写批处理文件并通过C#程序执行它 写入批处理文件: System.Diagnostics.ProcessStartInfo procinfo = new System.Diagnostics.ProcessStartInfo("cmd.exe"); procinfo.UseShellExecute = false; procinfo.RedirectStandardError = true;
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();
我将从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();
写入批处理文件后,我将文件名传递给下面的函数,该函数执行批处理文件以启动应用程序
问题:
我的程序将编写大量的批处理文件,这些文件在每个文件写入后立即执行。我发现,有时应用程序没有启动,这意味着批处理文件没有执行。对于批处理文件执行失败,我甚至没有收到任何错误消息或提示
预期解决方案:
如果在执行批处理文件时出现任何问题,我应该能够记录它或提示错误
执行批处理文件的代码:
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();
我不确定您的问题具体在哪里,但我对以下代码没有任何问题:
using (FileStream file = new FileStream("xyz.cmd", FileMode.Create)) {
using (StreamWriter sw = new StreamWriter(file)) {
sw.Write("@echo ====================\n");
sw.Close();
}
}
Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.FileName = "xyz.cmd";
//p.StartInfo.RedirectStandardOutput = true;
p.Start();
//String s = p.StandardOutput.ReadLine();
//while (s != null) {
// MessageBox.Show(s);
// s = p.StandardOutput.ReadLine();
//}
p.WaitForExit();
很明显,为了隐藏我的“秘密酱汁”,代码被删减了一点,但这是目前在生产中使用的代码,没有任何问题
我有一个问题。为什么不直接执行cmd
文件而不是运行cmd.exe
可能我要做的第一件事是打印出
BatchPath+LatestFileName
值,以查看您是否正在创建任何名称怪异的文件,这些文件将阻止cmd.exe
运行它们。我创建一个.Bat文件并在cmd.exe中执行。在这种情况下,XYZ.cmd在哪里?我是不是遗漏了什么?编写批处理文件后,我将其命名为“string.Format({0:dd-MM-yyyy[HH-MM-ss-fffff]}”,dt);”以使每个批处理文件名唯一。帮助…谢谢。@Anuya,您正在运行cmd.exe
,并将批处理文件的每一行传递给其标准输入。这似乎是一种复杂的方法。由于批处理文件已经在磁盘上,我只需运行它而不是cmd.exe
,这样您就不必担心向它发送单独的行了。只需将我给定代码中的xyz.cmd
替换为您自己的批处理文件名(基于日期/时间)。我的using
位就是为了向您展示我是如何创建批处理文件的。实际运行从进程p=…
位开始。