2013-08-22 27 views
0

我必須在打開我的主應用程序屏幕前獲得當前用戶權限。 由於我想確保只有管理員用戶(在獨立PC中)正在使用該應用程序。 我試過下面的代碼,它不能在Windows 7上工作(沒有在XP和其他版本中試過)。獲取當前登錄的Windows用戶特權

Dim identity = WindowsIdentity.GetCurrent() 
Dim principal = New WindowsPrincipal(identity) 
Dim isElevated As Boolean = principal.IsInRole(WindowsBuiltInRole.Administrator) 
Return isElevated 

是否有任何其他我需要引用的功能類。這已經很長時間了,我不確定有什麼問題。請幫忙。我需要它在Windows XP,Windows Vista,Windows 7上工作,如果可能的話,也可以在Windows 8上工作。

回答

0

下面的代碼片段正在爲我在Windows7中工作。你可以請嘗試一下。謝謝

public static bool IsAdminUser() 
{ 
    bool isAdmin = false; 
    AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal); 
    WindowsPrincipal principal = (WindowsPrincipal)System.Threading.Thread.CurrentPrincipal; 
    WindowsIdentity identity = (WindowsIdentity)principal.Identity; 
    isAdmin = principal.IsInRole(WindowsBuiltInRole.Administrator); 
    return isAdmin; 
} 
+0

我試過這個,但它沒有在我的VB.NET上工作。它是否適用於默認管理員或以管理員身份升級的用戶 – SOAMad

相關問題