C# 将参数传递给ProcessStartInfo类

C# 将参数传递给ProcessStartInfo类,c#,.net,processstartinfo,redirectstandardoutput,C#,.net,Processstartinfo,Redirectstandardoutput,我想使用Process.Start调用命令提示符命令,然后使用StandardOutput我想在我的应用程序中使用StreamReader读取,但是当我运行下面的程序时,在MessageBox中我只找到了Debug之前的路径,我在arguments中声明的命令不会执行 ProcessStartInfo info = new ProcessStartInfo("cmd.exe", "net view"); info.UseShellExecute = false;

我想使用Process.Start调用命令提示符命令,然后使用StandardOutput我想在我的应用程序中使用StreamReader读取,但是当我运行下面的程序时,在MessageBox中我只找到了Debug之前的路径,我在arguments中声明的命令不会执行

ProcessStartInfo info = new ProcessStartInfo("cmd.exe", "net view");
            info.UseShellExecute = false;
            info.CreateNoWindow = true;
            info.RedirectStandardOutput = true;    

            Process proc = new Process();
            proc.StartInfo = info;
            proc.Start();

            using(StreamReader reader = proc.StandardOutput)
            {
                MessageBox.Show(reader.ReadToEnd());
            }

此处我的网络视图命令从不执行。

如果要使用
cmd
运行命令,还必须指定
/c
参数:

new ProcessStartInfo("cmd.exe", "/c net view");
但是,在这种情况下,您根本不需要
cmd
net
是本机程序,可以按原样执行,无需外壳:

new ProcessStartInfo("net", "view");

如果要使用
cmd
运行命令,还必须指定
/c
参数:

new ProcessStartInfo("cmd.exe", "/c net view");
但是,在这种情况下,您根本不需要
cmd
net
是本机程序,可以按原样执行,无需外壳:

new ProcessStartInfo("net", "view");

还要记住拦截StandardErrorOutput,否则将什么也看不到:

var startInfo=newprocessstartinfo(“net”、“view”);
startInfo.UseShellExecute=false;
startInfo.CreateNoWindow=true;
startInfo.RedirectStandardError=true;
startInfo.RedirectStandardOutput=true;
使用(var process=process.Start(startInfo))
{
字符串消息;
使用(变量读取器=process.StandardOutput)
{
message=reader.ReadToEnd();
}
如果(!string.IsNullOrEmpty(消息))
{
MessageBox.Show(message);
}
其他的
{
使用(变量读取器=process.StandardError)
{
Show(reader.ReadToEnd());
}
}
}

还要记住拦截StandardErrorOutput,否则将什么也看不到:

var startInfo=newprocessstartinfo(“net”、“view”);
startInfo.UseShellExecute=false;
startInfo.CreateNoWindow=true;
startInfo.RedirectStandardError=true;
startInfo.RedirectStandardOutput=true;
使用(var process=process.Start(startInfo))
{
字符串消息;
使用(变量读取器=process.StandardOutput)
{
message=reader.ReadToEnd();
}
如果(!string.IsNullOrEmpty(消息))
{
MessageBox.Show(message);
}
其他的
{
使用(变量读取器=process.StandardError)
{
Show(reader.ReadToEnd());
}
}
}