如何檢查我的應用程序是以管理員權限開始的?我現在使用此代碼:如何檢查管理員權限C#
public static bool IsUserAdministrator()
{
//bool value to hold our return value
bool isAdmin;
try
{
//get the currently logged in user
WindowsIdentity user = WindowsIdentity.GetCurrent();
WindowsPrincipal principal = new WindowsPrincipal(user);
isAdmin = principal.IsInRole(WindowsBuiltInRole.Administrator);
}
catch (UnauthorizedAccessException ex)
{
isAdmin = false;
}
catch (Exception ex)
{
isAdmin = false;
}
return isAdmin;
}
此代碼檢查用戶權限,我需要檢查應用程序擁有的權限。例如,我不是管理員,但是當應用程序以管理員權限開頭時,此代碼返回false。謝謝!
可能重複的[我如何知道我的進程是否以管理員身份運行?](http://stackoverflow.com/questions/509292/how-can-i-tell-if-my-process-is-running -as-administrator) – msarchet
看不到'UnauthorizedAccessException'的原因等於'Exception' – WhileTrueSleep