我想在服务器上执行批处理文件,但问题是它无法执行。即使它没有给出任何错误。我该怎么办? 这是我的代码
try
{
ProcessStartInfo info = new ProcessStartInfo(AppPath + @"bin\execute.bat");
info.UseShellExecute = false;
info.RedirectStandardInput = true;
info.RedirectStandardError = true;
info.RedirectStandardOutput = true;
info.CreateNoWindow = false;
//info.WindowStyle = ProcessWindowStyle.Minimized;
info.WorkingDirectory = AppPath + @"bin";
using (Process install = Process.Start(info))
{
string output = install.StandardOutput.ReadToEnd();
install.WaitForExit();
Response.Write(output);
Console.WriteLine(output);
string strError = install.StandardError.ReadToEnd();
if (install.ExitCode == 0)
{
// Ok = true;
}
else
{
Response.Write(" Running failed. Description: " + strError);
}
}
}
catch (Win32Exception e)
{
Response.Write("W32 Error:" + e.NativeErrorCode.ToString() + "." + e.Message);
}
答案 0 :(得分:0)
尝试这样 - 你在代码中遗漏了一些指令。请仔细检查以下代码。
// Get the full file path
string strFilePath = “c:\\temp\\test.bat”;
// Create the ProcessInfo object
System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo("cmd.exe");
psi.UseShellExecute = false;
psi.RedirectStandardOutput = true;
psi.RedirectStandardInput = true;
psi.RedirectStandardError = true;
psi.WorkingDirectory = “c:\\temp\\“;
// Start the process
System.Diagnostics.Process proc = System.Diagnostics.Process.Start(psi);
// Open the batch file for reading
System.IO.StreamReader strm = System.IO.File.OpenText(strFilePath);
// Attach the output for reading
System.IO.StreamReader sOut = proc.StandardOutput;
// Attach the in for writing
System.IO.StreamWriter sIn = proc.StandardInput;
// Write each line of the batch file to standard input
while(strm.Peek() != -1)
{
sIn.WriteLine(strm.ReadLine());
}
strm.Close();
// Exit CMD.EXE
string stEchoFmt = "# {0} run successfully. Exiting";
sIn.WriteLine(String.Format(stEchoFmt, strFilePath));
sIn.WriteLine("EXIT");
// Close the process
proc.Close();
// Read the sOut to a string.
string results = sOut.ReadToEnd().Trim();
// Close the io Streams;
sIn.Close();
sOut.Close();
// Write out the results.
string fmtStdOut = "<font face=courier size=0>{0}</font>";
this.Response.Write(String.Format(fmtStdOut,results.Replace(System.Environment.NewLine, "<br>")));
答案 1 :(得分:0)
您可能需要提升运行应用程序的用户的权限。 Windows 2003阻止执行。批处理文件是本地还是网络位置?