我有以下代码从 AD 中删除本地用户帐户:
try
{
string username = "MyUserName";
using (DirectoryEntry hostMachineDirectory = new DirectoryEntry("WinNT://localhost"))
{
DirectoryEntries entries = hostMachineDirectory.Children;
DirectoryEntry deUser = null;
try
{
deUser = entries.Find(username, "User");
}
catch (COMException ex)
{
//Look for "no such user" exception
if ((uint)ex.ErrorCode != 0x800708ad)
{
throw ex;
}
}
if (deUser != null)
entries.Remove(deUser);
else
ShowMessageBoxError("No such user: " + username, MessageBoxIcon.Information);
}
}
catch (Exception ex)
{
ShowMessageBoxError(ex);
}
如果没有这样的用户,有什么方法可以避免引发和捕获该异常?