How do I get the current username in .NET using C#?

后端 未结 18 1858
忘了有多久
忘了有多久 2020-11-22 09:59

How do I get the current username in .NET using C#?

18条回答
  •  生来不讨喜
    2020-11-22 10:17

    I went over most of the answers here and none of them gave me the right user name.

    In my case I wanted to get the logged in user name, while running my app from a different user, like when shift+right click on a file and "Run as a different user".

    The answers I tried gave me the 'other' username.

    This blog post supplies a way to get the logged in user name, which works even in my scenario:
    https://smbadiwe.github.io/post/track-activities-windows-service/

    It uses Wtsapi

提交回复
热议问题