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

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

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

18条回答
  •  死守一世寂寞
    2020-11-22 10:08

    try this

    ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT UserName FROM Win32_ComputerSystem");
    ManagementObjectCollection collection = searcher.Get();
    string username = (string)collection.Cast().First()["UserName"];
    

    now it looks better

提交回复
热议问题