Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 显示局域网中的所有IP地址_C#_.net - Fatal编程技术网

C# 显示局域网中的所有IP地址

C# 显示局域网中的所有IP地址,c#,.net,C#,.net,我必须在列表框中显示局域网的所有IP地址。当我想把它绑起来的时候 //代码 Process netUtility = new Process(); netUtility.StartInfo.FileName = "net.exe"; netUtility.StartInfo.CreateNoWindow = true; netUtility.StartInfo.RedirectStandardOutput = true;

我必须在列表框中显示局域网的所有IP地址。当我想把它绑起来的时候

//代码

        Process netUtility = new Process(); 
        netUtility.StartInfo.FileName = "net.exe";

        netUtility.StartInfo.CreateNoWindow = true;


        netUtility.StartInfo.RedirectStandardOutput = true;

        netUtility.StartInfo.UseShellExecute = false;

        netUtility.StartInfo.RedirectStandardError = true;

        netUtility.Start();



        StreamReader streamReader = new StreamReader(netUtility.StandardOutput.BaseStream);



        string line = "";

        while ((line = streamReader.ReadLine()) != null)
        {

            if (line.StartsWith("\\"))
            {

                ListBox1.Items.Add(line.Substring(2).Substring(0, line.Substring(2).IndexOf(" ")).ToUpper());

            }

        }

        streamReader.Close();
        netUtility.WaitForExit(1000); 
  netUtility.StartInfo.Arguments = "view";

我哪里错了?

需要在流程中添加一行

//代码

        Process netUtility = new Process(); 
        netUtility.StartInfo.FileName = "net.exe";

        netUtility.StartInfo.CreateNoWindow = true;


        netUtility.StartInfo.RedirectStandardOutput = true;

        netUtility.StartInfo.UseShellExecute = false;

        netUtility.StartInfo.RedirectStandardError = true;

        netUtility.Start();



        StreamReader streamReader = new StreamReader(netUtility.StandardOutput.BaseStream);



        string line = "";

        while ((line = streamReader.ReadLine()) != null)
        {

            if (line.StartsWith("\\"))
            {

                ListBox1.Items.Add(line.Substring(2).Substring(0, line.Substring(2).IndexOf(" ")).ToUpper());

            }

        }

        streamReader.Close();
        netUtility.WaitForExit(1000); 
  netUtility.StartInfo.Arguments = "view";

现在它工作得很好

您可以简单地使用此方法,使其更加灵活和易于使用/理解:

C#代码: 通过此链接:


可能重复,请查看以下链接:[[1]: