Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Visual Basic .NET (http://www.programmingforums.org/forum19.html)
-   -   User Currently Logged in.... (http://www.programmingforums.org/showthread.php?t=11136)

randum77 Aug 21st, 2006 11:13 AM

User Currently Logged in....
 
Ok, yet another dumb question. I know i can pull what user is logged in by using a WMI pull. But how would I go about it with a System.Class.method? I've been searching the forums but can't find anything that helps.

I am trying to build an app that makes certian buttons active based on who's logged in. Any help is appreciated.

TIA

Infinite Recursion Aug 21st, 2006 1:32 PM

:

Module Module1

    Sub Main()
        Dim cUser As String
        cUser = GetUserName()
        Console.WriteLine("Current User: " + cUser)
    End Sub

    Declare Function GetUserName Lib "advapi32.dll" Alias _
      "GetUserNameA" (ByVal lpBuffer As String, _
      ByRef nSize As Integer) As Integer

    Public Function GetUserName() As String
        Dim iReturn As Integer
        Dim userName As String
        userName = New String(CChar(" "), 50)
        iReturn = GetUserName(userName, 50)
        GetUserName = userName.Substring(0, userName.IndexOf(Chr(0)))
    End Function

End Module


Or even easier...

:

Console.WriteLine("Current User: " + Environment.UserName)

randum77 Aug 21st, 2006 1:51 PM

Well damn, that sounds good to me. I can always count on you IR. :D

Infinite Recursion Aug 21st, 2006 3:34 PM

Glad I could help...

randum77 Aug 24th, 2006 1:42 PM

Quote:

Originally Posted by Infinite Recursion
:

Console.WriteLine("Current User: " + Environment.UserName)

IR, you rock. I just used the Environment.Username pull and put the username in a text box. Kick ass. Now all i have to do is a string compare and activate / deactivate buttons based on that. I guess I could use a ElseIf or Case Select.

Thank you a thousand times over.

Infinite Recursion Aug 24th, 2006 2:10 PM

Cool man, glad its working for you. Just a suggestion... have a list of acceptable users in a config file with the portions of the program they have access to... then enable/disable buttons based off of those entries... that way it would be more flexible. But, may not matter much as I'm not entirely sure what you are up to our how widespread its use will be.

randum77 Aug 31st, 2006 10:34 AM

Quote:

Originally Posted by Infinite Recursion
Cool man, glad its working for you. Just a suggestion... have a list of acceptable users in a config file with the portions of the program they have access to... then enable/disable buttons based off of those entries... that way it would be more flexible. But, may not matter much as I'm not entirely sure what you are up to our how widespread its use will be.

You sir about read my mind. *puts on tin foil beany* Here is the method I was using for button control based on username. It's not working, but it's my first excecution, so it's expected. Feel free to stomp it and offer alternatives.

:


        If usrName Like "BUT_%" Then
            Me.uiButRestButton3.Visible = True
            Me.uiConRestButton.Visible = False
            Me.uiNafRestButton.Visible = False
            Me.uiSGPRestButton.Visible = False
            Me.uiSSRestButton.Visible = False
            Me.uiTefRestButton.Visible = False
            Me.uiTraRestButton.Visible = False

        ElseIf usrName Like "%CON_%" Then
            Me.uiButRestButton3.Visible = False
            Me.uiConRestButton.Visible = True
            Me.uiNafRestButton.Visible = False
            Me.uiSGPRestButton.Visible = False
            Me.uiSSRestButton.Visible = False
            Me.uiTefRestButton.Visible = False
            Me.uiTraRestButton.Visible = False
.................................

        Else
            Me.uiButRestButton3.Visible = True
            Me.uiConRestButton.Visible = True
            Me.uiNafRestButton.Visible = True
            Me.uiSGPRestButton.Visible = True
            Me.uiSSRestButton.Visible = True
            Me.uiTefRestButton.Visible = True
            Me.uiTraRestButton.Visible = True

        End If


Bassically, we have alot of usernames that start w/ the same thing like. CONT_ or BUT_ ect..... and i am trying to make a catch all. I tried
:

if  usrName = "%but_%"

and

if usrName like "%but_%"


And of course, no love. I am sure it's something simple and my lack of experience is to blame. Thank you in advance.

Infinite Recursion Aug 31st, 2006 11:04 AM

Try substrings...

:

If usrName.Substring(0, 4).ToUpper = "BUT_" Then
    Me.uiButRestButton3.Visible = True
    ...


Substring(STARTING POSITION, NUM OF CHARS TO RETURN)
All strings start at 0, so for the %CON_% instance, you will need to determine where in the string CON_ starts...

For instance: USCON_MYID
:

usrName.Substring(2, 4).ToUpper = "CON_"

If the position of the keyword is not always constant in the username string, you could take the high road and use the .IndexOf string property to determine if the keyword is in that string... anywhere.

Another thing is... are you sure you want to disable the view property or just disable the ability to click the button? button.Visible = False vs button.Enabled = False

randum77 Aug 31st, 2006 12:39 PM

IR, i like the substring idea. I'll put that in. The 3 letter connotations apprear the same in all the names so it should work quite well. As for visibility, wer are doing this to cut down on technician time out in the area. The users are out in the area are used to doing MFG type line work, so they hardly know anything about computers, let alone what a start menue is. That's fine as long as they do the line work perfect most the time.

I am going to stack the buttons i have so it only appears as if there is one to any one user. Then i am going to control the visibility so that only the button they need for that area appears. Otherwise, they will either get confused or play around to much and muck up the configs for that area.

Once I am done I'll post up pic's of the final form so it will make more sense. Right now I have alot of unneeded output feilds on the form to verify all the gears are turning in the direction. :D This is turning out to be alot of fun for me. Thank you again.

randum77 Aug 31st, 2006 1:13 PM

Here's what the code ends up looking like when using the substring pull on the variable.

:


        Dim usrName As String

        'Who the currently logged in user
        usrName = Environment.UserName
        usrName = usrName.ToUpper
        Me.uiUserLabel2.Text = usrName


        'makes certian buttons available based on username
        If usrName.Substring(0, 3) = "BUT" Then
            Me.uiButRestButton3.Visible = True
            Me.uiConRestButton.Visible = False
            Me.uiNafRestButton.Visible = False
            Me.uiSGPRestButton.Visible = False
            Me.uiSSRestButton.Visible = False
            Me.uiTefRestButton.Visible = False
            Me.uiTraRestButton.Visible = False

        ElseIf usrName.Substring(0, 3) Like "CON" Then
            Me.uiButRestButton3.Visible = False
            Me.uiConRestButton.Visible = True
            Me.uiNafRestButton.Visible = False
            Me.uiSGPRestButton.Visible = False
            Me.uiSSRestButton.Visible = False
            Me.uiTefRestButton.Visible = False
            Me.uiTraRestButton.Visible = False
.
.
.
.
.

        Else
            Me.uiButRestButton3.Visible = True
            Me.uiConRestButton.Visible = True
            Me.uiNafRestButton.Visible = True
            Me.uiSGPRestButton.Visible = True
            Me.uiSSRestButton.Visible = True
            Me.uiTefRestButton.Visible = True
            Me.uiTraRestButton.Visible = True

        End If


It works great. I do apreciate it. Now I am moving on to the copy / progress screen part of it. I can copy the files, but i've been tumbling through and a progress that's not completely foreign to build.


All times are GMT -5. The time now is 10:25 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC