locked
Access VB Code to Read User Profile Directory on Windows System RRS feed

  • Question

  • User-266149547 posted

    This question is not .NET related, but I was hoping someone might be able to help with a simple access function that can return the name of a user's profile.

     For example, C:\Documents and Settings\jsmith

     return jsmith

    Thanks for any assistance.

    Friday, January 30, 2009 9:16 AM

Answers

  • User1052258516 posted

    Try using:

    Environment.UserName

    This will return the current UserName for the logged on user.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, January 30, 2009 10:05 AM

All replies

  • User1052258516 posted

    Try using:

    Environment.UserName

    This will return the current UserName for the logged on user.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, January 30, 2009 10:05 AM
  • User-266149547 posted

    Thanks.  Here is another method for anyone who needs the user name.

     

    Declare Function WNetGetUser Lib "mpr.dll" _
         Alias "WNetGetUserA" (ByVal lpName As String, _
         ByVal lpUserName As String, lpnLength As Long) As Long

    Const NoError = 0                    'The Function call was successful

    Function GetUserName() As String

         Dim LUserName As String
         Const lpnLength As Integer = 255
         Dim status As Integer
         Dim lpName

         ' Assign the buffer size constant to lpUserName.
         LUserName = Space$(lpnLength + 1)

         ' Get the log-on name of the person using product.
         status = WNetGetUser(lpName, LUserName, lpnLength)

         ' See whether error occurred.
         If status = NoError Then
              ' This line removes the null character. Strings in C are null-
              ' terminated. Strings in Visual Basic are not null-terminated.
              ' The null character must be removed from the C strings to be used
              ' cleanly in Visual Basic.
              LUserName = Left$(LUserName, InStr(LUserName, Chr(0)) - 1)

         Else
              ' An error occurred.
              MsgBox "Unable to get the name."
              End
         End If

         GetUserName = LUserName

    End Function

    Friday, January 30, 2009 12:13 PM