Reputation: 1011
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
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
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