0

我对编程很陌生,我有一个问题。我想获取网络上的计算机列表,我已经成功地做到了,然后我已经能够通过使用 .Count() 来获取有多少台计算机......问题是,我有几个完整的地方我想在其中引用该数字的代码,并且每次我尝试使用 c 时,它都会告诉我该变量在其上下文中不存在。我试图制作自己的公共方法,但随后它要求我在搜索后输入适当的回报。我仍然无法弄清楚..有人能指出我正确的方向吗?谢谢你。

public void ComputersOnNetwork()
{
   List<string> list = new List<string>();
   using (DirectoryEntry root = new DirectoryEntry("WinNT:"))
   {
       foreach (DirectoryEntry computer in computers.Children)
       {
           if ((computer.Name != "Schema"))
           {
               list.Add(computer.Name);
           }
       }

       foreach (string s in list)
       {
          int c = list.Count();
          return c;
       }       
4

4 回答 4

2

可能您在方法中遗漏了该return声明。您需要有一个返回类型(在这种情况下可能是整数类型作为您返回的计算机数量)

public int ComputersOnNetwork()
{        
  List<string> list = new List<string>();
  using (DirectoryEntry root = new DirectoryEntry("WinNT:"))
  {
       foreach (DirectoryEntry computer in computers.Children)
       {
          if ((computer.Name != "Schema"))
          {
             list.Add(computer.Name);
          }
       }
  }
  return list.Count;     
}

现在你可以像这样在任何你想要的地方调用它

int totalInThisCase=ComputersOnNetwork();
于 2012-05-22T23:26:22.547 回答
1

您必须有一个返回类型,并且您应该使用计数变量而不是添加到列表然后返回计数(另外,您为什么要这样迭代列表?您不需要这样做)。

始终从逻辑上阅读您的代码,并查看操作是否在逻辑上正确执行。

public int ComputersOnNetwork()
{
    int count = 0;

    using (DirectoryEntry root = new DirectoryEntry("WinNT:"))
    {
        foreach (DirectoryEntry computer in root.Children)
        {
            if ((computer.Name != "Schema"))
            {
                count++;
            }
        }

    return count;
}

或者您可以使用 LINQ 来创建一个简短的版本:

public int ComputersOnNetwork()
{
    using (var root = new DirectoryEntry("WinNT:"))
    {
        return root.Children.Cast<DirectoryEntry>().Count(x => x.Name != "Schema");
    }
}
于 2012-05-22T23:36:32.167 回答
0

There is no need to that final loop - if list contains all of the computers then this should do it:

  public void ComputersOnNetwork()
  {
       List<string> list = new List<string>();
       using (DirectoryEntry root = new DirectoryEntry("WinNT:"))
       {
           foreach (DirectoryEntry computer in computers.Children)
           {
               if ((computer.Name != "Schema"))
               {
                   list.Add(computer.Name);
               }
           }
       }

      return list.Count();
  } 

Return statements will stop the execution of a function as soon as you hit them, so your final for loop would only have run once.

Even without the return statement you would have had problems because c was declared within the loop and it would have gone out of scope, which is why you weren't able to reference it later on in your code. You need to be careful about declaring variables inside loops, they'll get re-defined on each iteration and although your code may compile it can be tricky to trace such bugs.

于 2012-05-22T23:34:08.497 回答
0

最后你不需要 foreach 循环。您从不使用变量“s”,因此它是浪费处理。

要获得您想要的内容,请更改您的方法以重新调整 int 而不是 void,然后在最后返回 list.Count。

public int ComputersOnNetwork()
{
   List<string> list = new List<string>();
   using (DirectoryEntry root = new DirectoryEntry("WinNT:"))
   {
       foreach (DirectoryEntry computer in computers.Children)
       {
           if ((computer.Name != "Schema"))
           {
               list.Add(computer.Name);
           }
       }

       int c = list.Count;
       return c;
   }
}

然后,您可以在需要该值时调用您的方法:

Int computerCount = ComputersOnNetwork();
于 2012-05-22T23:36:56.503 回答