locked
My.User.Name returns empty string. RRS feed

  • Question

  • I made a console application like this...

    Module Module1

    Sub Main()

    Debug.Print(String.Format("UserName = {0}", My.User.Name))

    Debug.Print(String.Format("IsAuthenticated = {0}", My.User.IsAuthenticated))

    End Sub

    End Module

    And all I get from the My.User.Name is an empty string... any ideas?

    I'm logged in to the network at my job so it shouldn't be because it thinks I'm not logged in.

     

     

     

     

     

     

    Tuesday, August 8, 2006 9:36 PM

Answers

  • If we do:

    My.User.InitializeWithWindowsUser()

    the My.User.Name gets assigned to correctly, so try this instead for the console application.

     

    Environment.Username gets the username (this is without running the command above) of the current thread started

    does this help?

    Tuesday, August 8, 2006 10:07 PM
  • You must call My.User.InitializeWithWindowsUser() before My.User will work properly in a console app (and most of the other project types except for a windows app).  In a windows app this is called automatiaclly.
    Tuesday, August 8, 2006 10:55 PM

All replies

  • what happens if you do Environment.Username?

    I believe My.User.Name will return empty is you are running Windows 95/98 as the OS does not recognize username

    Tuesday, August 8, 2006 9:48 PM
  • I am in Windows XP Professional SP 2

    .NET Framework 2.0

    Visual Studio 2005 Professional.

     

    I haven't tried Environment.UserName yet, But I did notice that when I ran the same statement in a WIndows Forms application instead of a console application, it works, so I am guessing that the My namespace isn't fully supported in Console apps.

    Do you know?

     

    Tuesday, August 8, 2006 9:58 PM
  • interesting.

    Namespaces should be supported fully regardless of the type of application , being it a winforms or console.

    I just tested this and I too have this problem. (And I am running WS2003EE SP1)

    I've also just read more in the documentation and states that yes this is supported in both winforms and console...I will investigate further

    Tuesday, August 8, 2006 10:03 PM
  • If we do:

    My.User.InitializeWithWindowsUser()

    the My.User.Name gets assigned to correctly, so try this instead for the console application.

     

    Environment.Username gets the username (this is without running the command above) of the current thread started

    does this help?

    Tuesday, August 8, 2006 10:07 PM
  • You must call My.User.InitializeWithWindowsUser() before My.User will work properly in a console app (and most of the other project types except for a windows app).  In a windows app this is called automatiaclly.
    Tuesday, August 8, 2006 10:55 PM
  • Thanks Jared - glad I was on the same tracks :-)
    Tuesday, August 8, 2006 10:56 PM
  •  

    Apparently wasn't being called automatically in my windows app, but after adding My.User.InitializeWithWindowsUser() it works fine now.

     

    The main project is C#. Maybe that has something to do with it?

    Wednesday, August 22, 2007 6:38 PM