从 cmd 读取输出得到错误
Reading Output from cmd getting errors
我一直在查找 "how to get standard output from cmd" 并找到了一些教程,但是 none,是的 NONE 似乎有效,我正在尝试阅读 [=19] 的所有输出=] 对我来说。
这里是全部代码,向下滚动查看错误位置
public static string C(string arguments, bool b)
{
System.Diagnostics.Process process = new
System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new
System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle =
System.Diagnostics.ProcessWindowStyle.Hidden;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.UseShellExecute = false;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = arguments;
process.StartInfo = startInfo;
process.Start();
string res = "";
if (b)
{
StringBuilder q = new StringBuilder();
while (!process.HasExited)
{
q.Append(process.StandardOutput.ReadToEnd());
}
string r = q.ToString();
res = r;
}
if(res == "" || res == null)
{
return "NO-RESULT";
}
return res;
}
哪里出现错误(System.InvalidOperationException:“StandardOut 尚未重定向或进程尚未启动。”)
string res = "";
StringBuilder q = new StringBuilder();
while (!process.HasExited)
{
q.Append(process.StandardOutput.ReadToEnd()); // Right here
}
string r = q.ToString();
res = r;
您正在创建一个名为 startInfo
的 ProcessStartInfo
,然后在 process.StartInfo
上设置一些属性,然后将 startInfo
分配给 process.StartInfo
基本上恢复您的设置以前。
您应该在 startInfo
上设置 RedirectStandardOutput
、RedirectStandardInput
和 UseShellExecute
:
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardInput = true;
startInfo.UseShellExecute = false;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = arguments;
process.StartInfo = startInfo;
process.Start();
我一直在查找 "how to get standard output from cmd" 并找到了一些教程,但是 none,是的 NONE 似乎有效,我正在尝试阅读 [=19] 的所有输出=] 对我来说。
这里是全部代码,向下滚动查看错误位置
public static string C(string arguments, bool b)
{
System.Diagnostics.Process process = new
System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new
System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle =
System.Diagnostics.ProcessWindowStyle.Hidden;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.UseShellExecute = false;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = arguments;
process.StartInfo = startInfo;
process.Start();
string res = "";
if (b)
{
StringBuilder q = new StringBuilder();
while (!process.HasExited)
{
q.Append(process.StandardOutput.ReadToEnd());
}
string r = q.ToString();
res = r;
}
if(res == "" || res == null)
{
return "NO-RESULT";
}
return res;
}
哪里出现错误(System.InvalidOperationException:“StandardOut 尚未重定向或进程尚未启动。”)
string res = "";
StringBuilder q = new StringBuilder();
while (!process.HasExited)
{
q.Append(process.StandardOutput.ReadToEnd()); // Right here
}
string r = q.ToString();
res = r;
您正在创建一个名为 startInfo
的 ProcessStartInfo
,然后在 process.StartInfo
上设置一些属性,然后将 startInfo
分配给 process.StartInfo
基本上恢复您的设置以前。
您应该在 startInfo
上设置 RedirectStandardOutput
、RedirectStandardInput
和 UseShellExecute
:
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardInput = true;
startInfo.UseShellExecute = false;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = arguments;
process.StartInfo = startInfo;
process.Start();