c# - Elegant way to check if a local group already exists -


is there way find out if local group exists? "best" solution works catching exception. there way achieve same goal without catching exception?

var machine = environment.machinename; var server = new directoryentry(string.format("winnt://{0},computer", machine)); bool groupexists = true; try {    server.children.find("mygroup", "group"); } catch (comexception ex) {    if (ex.errorcode == -2147022676)       groupexists = false;    else       throw; } 

you can try below code

        var machine = environment.machinename;         var server = new directoryentry(string.format("winnt://{0},computer", machine));         bool exists = server.children.cast<directoryentry>().any(d => d.schemaclassname.equals("group") && d.name.equals("administrators")); 

Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -