试试Catch Fail?

时间:2014-01-02 10:58:32

标签: c# wpf

我做了一些谷歌和东西,我似乎无法弄清楚这一点。 尝试Catch只是按照我期望的方式工作。

这是代码。

  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}

2 个答案:

答案 0 :(得分:1)

现在,您只是尝试检查Node.Properties.Count访问错误,仅此而已。代码范围DirectoryEntry Discover = new DirectoryEntry("WinNT://Workgroup");也必须放在try部分。

答案 1 :(得分:1)

试试这个

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
   }