2012-07-25 3 views
43

가능한 중복 : (OS 윈도우 XP/비스타/7은 C#으로 작성된 실행)
Check if the current user is administratorC#을 선택하면

나는 응용 프로그램이 경우 테스트 할 필요가

를 실행 관리자 권한으로 실행 (관리자 권한으로 실행 -> 관리자 권한으로 실행 또는 속성 아래의 권한 탭에서 관리자 권한으로 실행).

내가 봤 거든 StackOverflow 검색했지만 작동 해결책을 찾을 수 없습니다.

if ((new WindowsPrincipal(WindowsIdentity.GetCurrent())) 
     .IsInRole(WindowsBuiltInRole.Administrator)) 
{ 
    ... 
} 
+1

이는 UAC 것입니까? 나는. 사용자는 이미 관리자이지만 앱이 UAC 하에서 승격되었는지 알고 싶습니까? – spender

+2

중복되지 않습니다. 이 질문은 로그인 한 사용자가 아닌 프로세스에 대해 묻습니다. –

답변

79

이 기능적 코드와 같은 보이지만, 위의

... 나를 위해 노력하고 있습니다 일이

public static bool IsAdministrator() 
{ 
    var identity = WindowsIdentity.GetCurrent(); 
    var principal = new WindowsPrincipal(identity); 
    return principal.IsInRole(WindowsBuiltInRole.Administrator); 
} 

을 시도해보십시오

내 마지막 시도는이했다 기능적으로 (불필요한 임시 변수없이) ...

public static bool IsAdministrator() 
{ 
    return (new WindowsPrincipal(WindowsIdentity.GetCurrent())) 
      .IsInRole(WindowsBuiltInRole.Administrator); 
} 
,210

또는 사용하여 표현 바디 특성 :

public static bool IsAdministrator => 
    new WindowsPrincipal(WindowsIdentity.GetCurrent())) 
     .IsInRole(WindowsBuiltInRole.Administrator); 
+13

"using System.Security.Principal;"을 반드시 포함하십시오. – LightLabyrinth

+0

Windows 10에서 나를 위해 일했습니다. – Alexander

+0

using 문을 사용하여 포장해야합니다. "using (var identity = WindowsIdentity.GetCurrent())" – zezba9000

관련 문제