我如何获得当前用户名在。net使用c# ?


当前回答

String myUserName = Environment.UserName

这将为您提供输出- your_user_name

其他回答

试试这个

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

现在看起来好多了

试试这个属性:Environment.UserName。

获取当前Windows用户名:

using System;

class Sample
{
    public static void Main()
    {
        Console.WriteLine();

        //  <-- Keep this information secure! -->
        Console.WriteLine("UserName: {0}", Environment.UserName);
    }
}

你可能还想尝试使用:

Environment.UserName;

像这样…:

string j = "Your WindowsXP Account Name is: " + Environment.UserName;

希望这对你有所帮助。

string userName = System.Security.Principal.WindowsIdentity.GetCurrent().Name;