2014-02-07 34 views
3

我在程序中使用net.exe来查看工作组中的所有计算机。NET VIEW在与Process.Start()一起使用时行为有所不同()

的代码如下:当我在shell执行

var net = new Process(); 

    net.StartInfo.FileName = "net.exe"; 
    net.StartInfo.CreateNoWindow = true; 
    net.StartInfo.Arguments = @"VIEW /DOMAIN:my-workgroup"; 
    net.StartInfo.RedirectStandardOutput = true; 
    net.StartInfo.UseShellExecute = false; 
    net.StartInfo.RedirectStandardError = true; 
    net.Start(); 

命令工作正常,但是当我使用的代码所示,该命令将返回the device is not connected

我也尝试以管理员身份运行该程序,这没有什么区别。

指定的域名实际上是一个工作组

对于net.exe在shell中运行指定工作组工作正常。

此外,当我为另一个域尝试net view时,该代码也适用。所以当我从shell或Process.Start()运行命令时,环境中必定有所不同。

这个命令在shell中的行为与Process.Start()有什么不同?

+0

当我采取这一确切的代码,并在我的耐刮划运行它,它运行'net'就好了。你确定它是*这个*代码产生的错误? –

+0

@Damien_The_Unbeliever是的,我绝对乐观。 – thumbmunkeys

+0

只用普通的/ DOMAIN运行它,没有名称,所以你至少会得到一个有效的域名列表。 –

回答

0

我不知道它是否会解决你要找的东西,但这对我有用;

您需要连接到捕捉输出将其带到控制台

class Program 
{ 
    static void Main(string[] args) 
    { 
     var net = new Process() 
     { 
      StartInfo = new ProcessStartInfo("net.exe", @"view /domain:domain") 
      { 
       RedirectStandardOutput = true, 
       UseShellExecute = false, 
      }, 


     }; 
     net.OutputDataReceived += WriteToConsole; 
     net.ErrorDataReceived += WriteToConsole; 
     net.Start(); 
     net.BeginOutputReadLine(); 

     net.WaitForExit(); 
     Console.ReadLine(); 
    } 

    private static void WriteToConsole(object sender, DataReceivedEventArgs e) 
    { 
     Console.WriteLine(e.Data); 
    } 
} 
+0

谢谢,我试过你的代码,不幸的是我得到了与我的代码 – thumbmunkeys

+2

相同的结果奇怪,抱歉不知道还有什么建议 – owen79

相关问题