locked
How do i capture windows login name for windows service? RRS feed

  • Question

  • Hi,

    I would like to ask if i intend to copy or capture the windows login name, how am i to go about doing it?

    I tried

    string Name = Environment.GetEnvironmentVariable("USERNAME");
    string FirstPresentTime = DateTime.Now.ToString();

    then write it to txt file but the name is blank but the Date&time appeared and if i change it to

    string Name = ("USER");

    the USER and Date&time will both appear.

    Thanks

    Wednesday, August 12, 2009 8:41 AM

Answers

All replies

  • WindowsIdentity.GetCurrent().Name

            -Steve
    Programming blog: http://nitoprograms.blogspot.com/
      Including my TCP/IP .NET Sockets FAQ
    MSBuild user? Try out the DynamicExecute task in the MSBuild Extension Pack source; it's currently in Beta so get your comments in!
    I will be at the Grand Rapids BarCamp 4 unconference - come on by, fellow Michiganders!
    • Proposed as answer by Joshua Bylotas Wednesday, August 12, 2009 9:56 PM
    • Marked as answer by Bin-ze Zhao Monday, August 17, 2009 5:43 AM
    Wednesday, August 12, 2009 2:39 PM
  • Hi Steve, I tried the method u provided but i got error when i build it, The name "WindowsIdentity" does not exist in the current context. then i added using System.Security.Principal; don't know if its correct but it can build but when i debugged it its the value is "NT AUTHORITY\\SYSTEM". Help me on this, Thanks
    Thursday, August 13, 2009 1:40 AM
  • Hi Steve, I tried the method u provided but i got error when i build it, The name "WindowsIdentity" does not exist in the current context. then i added using System.Security.Principal; don't know if its correct but it can build but when i debugged it its the value is "NT AUTHORITY\\SYSTEM". Help me on this, Thanks
    Thursday, August 13, 2009 1:40 AM
  • It gets the current user name. If your program is running as the local system (e.g., a Windows Service or scheduled via the at command), then you should get that result.

           -Steve


    Programming blog: http://nitoprograms.blogspot.com/
      Including my TCP/IP .NET Sockets FAQ
    MSBuild user? Try out the DynamicExecute task in the MSBuild Extension Pack source; it's currently in Beta so get your comments in!
    I will be at the Grand Rapids BarCamp 4 unconference - come on by, fellow Michiganders!
    Thursday, August 13, 2009 3:40 AM