C# 尝试捕捉失败?

C# 尝试捕捉失败?,c#,wpf,C#,Wpf,我做了一些谷歌之类的东西,但我似乎无法理解这一点。 试着接球并没有达到我预期的效果 这是代码 DirectoryEntry Discover = new DirectoryEntry("WinNT://Workgroup"); foreach (DirectoryEntry Node in Discover.Children) { try { if (Node.Properties.Count >

我做了一些谷歌之类的东西,但我似乎无法理解这一点。 试着接球并没有达到我预期的效果

这是代码

  DirectoryEntry Discover = new DirectoryEntry("WinNT://Workgroup");
        foreach (DirectoryEntry Node in Discover.Children)
        {
            try {
                if (Node.Properties.Count > 0)
                {
                    //you have access to the computer so yeah
                }
            }
            catch(System.IO.FileNotFoundException err) {
                var ErrorMesage = err.Message;
                //Yeah you dont have access to this computer, lets write a subroutine to allow the user to put in a username and password to access it
            }
所以当我运行这个程序时,我会弹出一个史诗般的对话框,然后说一些愚蠢的话,比如


{“未找到网络路径。\r\n”:null}

现在您只需尝试检查
节点.Properties.Count
访问是否有错误,仅此而已。代码范围
DirectoryEntry Discover=newdirectoryEntry(“WinNT://Workgroup”)也必须放在
try
部分。

试试这个

try {
       DirectoryEntry Discover = new DirectoryEntry("WinNT://Workgroup");  **//Error occured here**
        foreach (DirectoryEntry Node in Discover.Children)
        {                
                if (Node.Properties.Count > 0)
                {
                    //you have access to the computer so yeah
                }
         }
    }
   catch(System.IO.FileNotFoundException err) 
   {
                var ErrorMesage = err.Message;
                //Yeah you dont have access to this computer, lets write a subroutine to allow the user to put in a username and password to access it
   }

确定您收到的是FileNotFoundException?您的计算机是否通过工作组连接?并确保工作组名称为“WorkGroup”,检查
DirectoryEntry
实例是否正确创建-是,因为我可以在node.Name中看到计算机名称。计算机位于同一工作组中。