Is there a way to find out if a local group already exists? My "best" solution works by catching an exception. Is there a way to do achieve the same goal without catching an 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 the below code