简体   繁体   English

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

[英]How to run nested batch file programatically using C#

I am trying to execute a batch through my Client/server socket program. 我正在尝试通过客户端/服务器套接字程序执行批处理。 Inside the main batch file, it calls other batch files . 在主批处理文件中,它调用其他批处理文件。 All batch files exist in the same folder. 所有批处理文件都存在于同一文件夹中。 The Server program application is in exist another folder (E:\\Apps) and running it as windows service. 服务器程序应用程序位于另一个文件夹(E:\\ Apps)中,并作为Windows服务运行。

Main batch file Path : 主批处理文件路径:

E:\\MyApp\\UAT\\guest\\mytasks\\Main.bat E:\\ MyApp \\ UAT \\ guest \\ mytasks \\ Main.bat

Inside the main batch file below calls are made 在主批处理文件中,进行以下调用

call E:\\MyApp\\UAT\\guest\\mytasks\\stop.bat 呼叫E:\\ MyApp \\ UAT \\ guest \\ mytasks \\ stop.bat

call %mytasks%\\start.bat 呼叫%mytasks%\\ start.bat

When I execute the Main.bat file, my program returns the below error and not the executing rest of the code in the Main.bat file. 当我执行Main.bat文件时,我的程序返回以下错误,而不是Main.bat文件中正在执行的其余代码。

'.\\stop.bat' is not recognized as an internal or external command, '。\\ stop.bat'不被识别为内部或外部命令,

Function call 函数调用

ExecuteShellCommand(": E:\\MyApp\\UAT\\guest\\mytasks\\Main.bat", "E:\\", ref Output, ref Error); ExecuteShellCommand(“:E:\\ MyApp \\ UAT \\ guest \\ mytasks \\ Main.bat”,“ E:\\”,参考输出,参考错误);

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;
}

} }

'.\\stop.bat' is not recognized as an internal or external command, means that stop.bat isn't being found in your current active directory, or the directory the original batch file was executed in. However, this may not be the case because the actual process that executed the batch file was in C# program, and may not be in the same directory as the stop.bat file. '.\\stop.bat' is not recognized as an internal or external command,意味着在当前的活动目录或原始批处理文件所在的目录中找不到stop.bat。但是,可能不会之所以如此,是因为执行批处理文件的实际进程在C#程序中,并且可能与stop.bat文件不在同一目录中。 Try putting it into the same directory as the C# program. 尝试将其放入与C#程序相同的目录中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM