如何使用C#以编程方式运行嵌套批处理文件

时间:2013-08-14 17:38:12

标签: c# batch-file process

我正在尝试通过我的客户端/服务器套接字程序执行批处理。在主批处理文件中,它调用其他批处理文件。所有批处理文件都存在于同一文件夹中。服务器程序应用程序存在于另一个文件夹(E:\ Apps)中,并将其作为Windows服务运行。

主批处理文件路径:

  

E:\ MyApp的\ UAT \旅客\ 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;
}

}

1 个答案:

答案 0 :(得分:0)

'.\stop.bat' is not recognized as an internal or external command,表示在当前的活动目录中找不到stop.bat,或者在原始批处理文件的执行目录中找不到。但是,这可能不是这种情况,因为实际执行的是批处理文件位于C#程序中,可能与stop.bat文件不在同一目录中。尝试将其放入与C#程序相同的目录中。