我试图通过我的客户端/服务器套接字程序执行批处理。 在主批处理文件中,它调用其他批处理文件。 所有批处理文件都存在于同一文件夹中。 服务器程序应用程序存在于另一个文件夹(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'未被识别为内部或外部命令,
函数调用
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; }ExecuteShellCommand(“:E:\ MyApp \ UAT \ guest \ mytasks \ Main.bat”,“E:\”,ref Output,ref Error);
}
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.
Main batch file Path :
E:\MyApp\UAT\guest\mytasks\Main.bat
Inside the main batch file below calls are made
call E:\MyApp\UAT\guest\mytasks\stop.bat
call %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.
'.\stop.bat' is not recognized as an internal or external command,
Function call
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; }ExecuteShellCommand(": E:\MyApp\UAT\guest\mytasks\Main.bat", "E:\", ref Output, ref Error);
}
最满意答案
'.\stop.bat' is not recognized as an internal or external command,意味着在当前的活动目录中找不到stop.bat,或者在原始批处理文件的执行目录中找不到。但是,这可能不是这种情况是因为执行批处理文件的实际进程是在C#程序中,并且可能与stop.bat文件不在同一目录中。 尝试将其放入与C#程序相同的目录中。
'.\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. Try putting it into the same directory as the C# program.
更多推荐
发布评论