Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > How to check the user loged On Windows
 

How to check the user loged On Windows

Hi there i need to check windows loged in user to determin the privilages:

If logedin User = Administrator then

btn1.Visible=true
else

btn1.Visible=False

thanks
deti  Monday, July 06, 2009 11:20 AM

Hi Deti,

Please try the following sample code:

Imports System.Security.Principal

Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim wi As WindowsIdentity = WindowsIdentity.GetCurrent()
Dim wp As New WindowsPrincipal(wi)
Dim result As Boolean = wp.IsInRole(WindowsBuiltInRole.Administrator)
If result then
MessageBox.Show("The current user is anAdmin.")
else
MessageBox.Show("The current useris not an Admin.")
End If
End Sub
End Class

In addition, if you run this code on Windows Vista, you need to run the application as Administrator so that the result variable returns true.

Hope thishelps.
If you have any question, please feel free to let me know.

Sincerely,
Linda Liu


Please remember to mark the replies as answers if they help and unmark them if they provide no help. end us any feedback you have about the help from MSFT at fbmsdn@microsoft.com.
  • Marked As Answer bydeti Thursday, July 16, 2009 7:58 AM
  •  
Linda Liu  Thursday, July 16, 2009 7:48 AM
Wollinet  Monday, July 06, 2009 12:00 PM
I Did but i get this :

_isAdministrator

is not defined


(I Did Converted to VB )

deti  Monday, July 06, 2009 1:47 PM
bool _isAdministrator = ....


or probably in VB:

Dim _isAdministrator as Boolean
Wollinet  Monday, July 06, 2009 2:21 PM
thanks that is done ,, now i get this error:

wid

As New WindowsIdentity(Environment.UserName) Incorrect function.

i am using xp sp2 does that afect any thing?

deti  Monday, July 06, 2009 2:31 PM

Hi Deti,

Please try the following sample code:

Imports System.Security.Principal

Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim wi As WindowsIdentity = WindowsIdentity.GetCurrent()
Dim wp As New WindowsPrincipal(wi)
Dim result As Boolean = wp.IsInRole(WindowsBuiltInRole.Administrator)
If result then
MessageBox.Show("The current user is anAdmin.")
else
MessageBox.Show("The current useris not an Admin.")
End If
End Sub
End Class

In addition, if you run this code on Windows Vista, you need to run the application as Administrator so that the result variable returns true.

Hope thishelps.
If you have any question, please feel free to let me know.

Sincerely,
Linda Liu


Please remember to mark the replies as answers if they help and unmark them if they provide no help. end us any feedback you have about the help from MSFT at fbmsdn@microsoft.com.
  • Marked As Answer bydeti Thursday, July 16, 2009 7:58 AM
  •  
Linda Liu  Thursday, July 16, 2009 7:48 AM
thanks that is perfect
deti  Thursday, July 16, 2009 7:58 AM

You can use google to search for other answers

Custom Search

More Threads

• How to populate a datagridview with columns in my SQL.mdf, by using a checkedlistbox to select the columns at runtime.
• DataBind with Customization in Datagridview
• DataGridView performance issue
• How do assign values into the RowFilter Property of DatagridView class?
• Any Comprehensive C#/ADO.NET Samples out there?
• DataGridView ComboBoxCell Problem
• Listview allowcolumnreorder and sorting
• Change string column to checkbox column
• Error: LookupBox.Dispose: no suitable method in LookupControlWalkthrough sample
• I have some Problem ProcessCmdKey?