New Bee
New Bee

Reputation: 1011

Try Catch Fail?

Ive done some google and stuff and i cant seem to figure this out. Try Catch just doesnt work the way i would expect it to.

here is the code.

  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
            }

so when i run this i get a epic box popup and say something stupid like

{"The network path was not found.\r\n":null}

Upvotes: 1

Views: 279

Answers (2)

Nilesh Gajare
Nilesh Gajare

Reputation: 6398

Try this

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
   }

Upvotes: 1

Illia Tereshchuk
Illia Tereshchuk

Reputation: 1202

Now you are only attempt to check Node.Properties.Count access for errors, nothing more. Scope of the code DirectoryEntry Discover = new DirectoryEntry("WinNT://Workgroup"); has also to be put in try section.

Upvotes: 1

Related Questions