我正在尝试通过我的客户端/服务器套接字程序执行批处理。在主批处理文件中,它调用其他批处理文件。所有批处理文件都存在于同一文件夹中。服务器程序应用程序存在于另一个文件夹 (E:\Apps) 中,并将其作为 Windows 服务运行。
主批处理文件路径:
E:\MyApp\UAT\guest\mytasks\Main.bat
在主批处理文件中进行以下调用
调用 E:\MyApp\UAT\guest\mytasks\stop.bat
调用 %mytasks%\start.bat
当我执行 Main.bat 文件时,我的程序返回以下错误,而不是 Main.bat 文件中执行的其余代码。
'.\stop.bat' 未被识别为内部或外部命令,
函数调用
ExecuteShellCommand(": E:\MyApp\UAT\guest\mytasks\Main.bat", "E:\", ref Output, ref Error);
private static void ExecuteShellCommand(string _FileToExecute, string _CommandLine, ref string _outputMessage, ref string _errorMessage)
{
// Set process variable
// Provides access to local and remote processes and enables you to start and stop local system processes.
System.Diagnostics.Process eodProcess = null;
try
{
eodProcess = new System.Diagnostics.Process();
// invokes the cmd process specifying the command to be executed.
string _CMDProcess = string.Format(System.Globalization.CultureInfo.InvariantCulture, @"{0}\cmd.exe", new object[] { Environment.SystemDirectory });
// pass executing file to cmd (Windows command interpreter) as a arguments
// /C tells cmd that we want it to execute the command that follows, and then exit.
string _Arguments = string.Format(System.Globalization.CultureInfo.InvariantCulture, "/C {0}", new object[] { _FileToExecute });
// pass any command line parameters for execution
if (_CommandLine != null && _CommandLine.Length > 0)
{
_Arguments += string.Format(System.Globalization.CultureInfo.InvariantCulture, " {0}", new object[] { _CommandLine, System.Globalization.CultureInfo.InvariantCulture });
}
// Specifies a set of values used when starting a process.
System.Diagnostics.ProcessStartInfo eodProcessStartInfo = new System.Diagnostics.ProcessStartInfo(_CMDProcess, _Arguments);
// sets a value indicating not to start the process in a new window.
eodProcessStartInfo.CreateNoWindow = true;
// sets a value indicating not to use the operating system shell to start the process.
eodProcessStartInfo.UseShellExecute = false;
// sets a value that indicates the output/input/error of an application is written to the Process.
eodProcessStartInfo.RedirectStandardOutput = true;
eodProcessStartInfo.RedirectStandardInput = true;
eodProcessStartInfo.RedirectStandardError = true;
eodProcess.StartInfo = eodProcessStartInfo;
// Starts a process resource and associates it with a Process component.
eodProcess.Start();
// Instructs the Process component to wait indefinitely for the associated process to exit.
_errorMessage = eodProcess.StandardError.ReadToEnd();
eodProcess.WaitForExit();
// Instructs the Process component to wait indefinitely for the associated process to exit.
_outputMessage = eodProcess.StandardOutput.ReadToEnd();
eodProcess.WaitForExit();
}
catch (Exception _Exception)
{
// Error
Console.WriteLine("Exception caught in process: {0}", _Exception.ToString());
}
finally
{
// close process and do cleanup
eodProcess.Close();
eodProcess.Dispose();
eodProcess = null;
}
}