Registry Keys with spaces

I am trying to retrieve the username from the registry which unless there is a better one I used HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Logon User Name the problem is I can't figure out how to deal with the spaces I tried retrieving other keys without the spaces and they are just fine. Is there a String function or something that I can put in to deal with the spaces or is there a better registry key for the user currently logged into the system

Heres the code I got.

            Dim regKey As RegistryKey = Registry.CurrentUser
            regKey = regKey.OpenSubKey("Software\Microsoft\Windows\CurrentVersion\Explorer", False)
            Dim strUsername As String = regKey.GetValue("Logon User Name", "Unknown User").ToString



Answer this question

Registry Keys with spaces

  • Mitch Walker - MSFT

    My.User.Name

    It returns the Name of the currently logged on user



  • &#59;lkj&#59;lk

    Yes that works. Thanks. I didn't know about that one I only found CURRENT_USER which was no help.

  • fscarpa58

    What error are you getting or problem are you having ...I tested your code "AS IS" on my machine and the messagebox showed my name

    Dim regKey As RegistryKey = Registry.CurrentUser

    regKey = regKey.OpenSubKey("Software\Microsoft\Windows\CurrentVersion\Explorer", False)

    Dim strUsername As String = regKey.GetValue("Logon User Name", "Unknown User").ToString

    MessageBox.Show(strUsername)

    WAG: make sure your application is setup as a full trust app



  • crash33

    well, it is not an issue with spaces in the key name...because like I said...it works for me...have you opened regedit and checked to make sure you have a value in the key

    Another option you may want to look into:

    System.Security.Principal.WindowsIdentity.GetCurrent().Name



  • nelpasa

    I must admit that I haven't worked with SQL projects, but can't you use the T-SQL command SYSTEM_USER

    Best regards,
    Johan Stenberg



  • Socrates Kapetaneas

    THe problem I am getting is that it is always giving me the default value Unknown User. WHen I tried some other random key without spaces it wrote the right value. I have the application set on external_access permissions.

  • ClydeCoulter

    Yeah it has a value. I tried your other suggestion and I get the same result as I would with Environment.UserName. NT AUTHORITY\SYSTEM is the name I get. It was to be something to do with the spaces because all the other registry keys without spaces produces a value. Maybe it is because it is a SQL Server Project is the only other reason I can think of. I really need to get this username somehow.

  • Wayne Sepega

    I can't use that because this is a sql server project part of my solution and it won't let you reference non SQL Server Projects. I tried Environment.UserName but that just returns the name of the account running the process which is SYSTEM. Registry is the only why I could find to do this.

  • Registry Keys with spaces