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
Post a Comment