C# 如何包含批处理文件命令?
我有以下调用和执行批处理文件的代码C# 如何包含批处理文件命令?,c#,batch-file,C#,Batch File,我有以下调用和执行批处理文件的代码 System.Diagnostics.Process proc = new System.Diagnostics.Process(); proc.StartInfo.FileName = @"C:\tS\comm.bat"; proc.StartInfo.RedirectStandardError = false; proc.StartInfo.RedirectStandardOutput = true; proc.StartInfo.UseShellExec
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo.FileName = @"C:\tS\comm.bat";
proc.StartInfo.RedirectStandardError = false;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.UseShellExecute = false;
proc.Start();
string strGetInfo = proc.StandardOutput.ReadToEnd();
strCMDOut = strGetInfo.Substring(strGetInfo.Length - 5, 3);
//MessageBox.Show(cmdOut);
proc.WaitForExit();
comm.bat
文件包含以下内容:
@ECHO ON
java com.test.this send
与其调用文件来执行bat文件,我如何将其合并到我的C#代码中并防止任何文件问题?(我需要将输出保存为字符串,因为在上面的代码中它已经在这样做了。)
另外,我如何以静默方式进行操作,以便用户看不到CMD窗口,并且操作在后台进行
我将代码替换为:
var proc = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = "cmd.exe",
Arguments = "java com.test.this send",
RedirectStandardError = false,
UseShellExecute = false,
RedirectStandardOutput = true,
CreateNoWindow = true
}
};
proc.Start();
string strGetInfo = proc.StandardOutput.ReadToEnd();
strCMDOut = strGetInfo.Substring(strGetInfo.Length - 5, 3);
MessageBox.Show(strCMDOut);
我在消息框中得到了一个
,而不是原始代码显示的代码。这可能足以:
var pro = Process.Start("java", "com.test.this send");
考虑到您在上有@ECHO,我假定您希望读取进程的输出(以防它从进程内部重定向)
为此,请阅读:
如果这不是您想要的,请澄清。关于,
我如何以静默方式进行操作,以便用户看不到CMD窗口,并且操作在后台进行
通过设置ProcessStartInfo的此属性,可以抑制新窗口
CreateNoWindow = true
从C程序中启动的进程的批处理文件调用java程序。。。我怀疑这是解决问题的正确方法。com.test.this
实际上做了什么?你能在C#中复制它的函数吗?它到目前为止一直在工作,只是不想调用批处理文件并将其嵌入到我的C#应用程序中。@ChrisDunaway不幸的是,它是一个供应商应用程序,与任何C#代码都不兼容。用我的代码,我已经在做了。我可以读取输出并将其保存为字符串,以便以后在应用程序中使用。我只是想消除读取bat文件的麻烦,并将其嵌入到我的C#代码中。@SiKni8:所以尝试像链接示例中那样对其进行调用。这似乎是我要做的,我如何添加多个参数?@SiKni8:根据文档,它是一个字符串,所以只需在单个字符串中传递它们,如示例所示。我用我所拥有的更新了我的问题,得到一个
作为消息框。