Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如果帐户名已更改,则为当前用户名_C#_.net_Windows Authentication - Fatal编程技术网

C# 如果帐户名已更改,则为当前用户名

C# 如果帐户名已更改,则为当前用户名,c#,.net,windows-authentication,C#,.net,Windows Authentication,我试图在c#中获取当前登录用户的名称,而不是在Environment.UserName中很容易找到的帐户名称。 我想像explorer那样枚举我计算机上的文件夹。我如何才能做到这一点,或者是否有其他方法可以获得正确的用户名 提前感谢。使用它 string windowLoging = WindowsIdentity.GetCurrent().Name; 或 或 尝试使用: System.Security.Principal.WindowsIdentity.GetCurrent().Name这应

我试图在c#中获取当前登录用户的名称,而不是在Environment.UserName中很容易找到的帐户名称。 我想像explorer那样枚举我计算机上的文件夹。我如何才能做到这一点,或者是否有其他方法可以获得正确的用户名

提前感谢。

使用它

string windowLoging = WindowsIdentity.GetCurrent().Name;

尝试使用:

System.Security.Principal.WindowsIdentity.GetCurrent().Name
这应返回当前登录用户的帐户及其名称

如果用户名更改后该方法不起作用,另一种方法是获取当前用户的SID,然后查找与该SID匹配的用户名

using System.Security.Principal;
string sid = WindowsIdentity.GetCurrent().Owner.ToString();
return new SecurityIdentifier(sid).Translate(typeof(NTAccount)).ToString();
如果失败,请获取SID并尝试通过WMI或注册表查找匹配的用户。有关如何手动执行此操作的说明如下:

如果您可以手动确认这两个方法中的任何一个返回新用户名,那么只需在代码中通过WMI调用或注册表访问来实现即可。

@daminice88
您的链接使我找到了解决方案:

    SelectQuery query = new SelectQuery("Win32_UserAccount", string.Format("Domain='{0}'", Environment.MachineName)); 
    ManagementObjectSearcher searcher = new ManagementObjectSearcher(query); 
    foreach (ManagementObject mObject in searcher.Get()) 
    { 
        Console.WriteLine((string)mObject["Name"] + "\t" + (string)mObject["FullName"]); 
    } 

“全名”是我搜索过的属性。非常感谢Harald Pitro

提供你所做的。我不确定你在这里要求什么。你能提供一个更具说明性的例子来说明你想做什么吗?我特别不知道“我想像explorer那样枚举我计算机上的文件夹”是什么意思,以及它如何与您的问题的其余部分相适应。应该用它捕获
SecurityException
(可能返回您现在使用的环境名称作为后备)您的链接将我引向解决方案:SelectQuery query=newselectquery(“Win32_UserAccount”,string.Format(“Domain='{0}',Environment.MachineName));ManagementObjectSearcher search=新的ManagementObjectSearcher(查询);foreach(searcher.Get()中的ManagementObject mObject){Console.WriteLine((字符串)mObject[“Name”]+“\t”+(字符串)mObject[“FullName”];}“FullName”是我搜索的属性。非常感谢。您应该将该解决方案作为答案发布。在计算它的出口方面做得很好,比如说我们在机器上有一个名为“Admin”的帐户。如果用户在“控制面板”中将此用户帐户的名称更改为“Harald”,则资源管理器会将“我的文档”文件夹显示为“Harald”而不是“Admin”。像Identity.name或Environment.UserName这样的所有函数的结果都是“Admin”。如何获取当前登录用户的已更改用户名(即:“Harald”)?所有这些代码都提供原始用户名,而不是已更改的帐户名。在示例中,所有这些代码都提供了“Admin”-但我正在搜索一种获取当前名称“Admin”的方法-这是it Explorer显示的“Harald”。能否显示您的代码,您是如何更改帐户名的?没有代码。我使用控制面板-用户管理,并有“更改自己的用户名”。这就是登录屏幕上显示的名称(即“Harald”)。帐户本身保持在“Admin”状态。
using System.Security.Principal;
string sid = WindowsIdentity.GetCurrent().Owner.ToString();
return new SecurityIdentifier(sid).Translate(typeof(NTAccount)).ToString();
    SelectQuery query = new SelectQuery("Win32_UserAccount", string.Format("Domain='{0}'", Environment.MachineName)); 
    ManagementObjectSearcher searcher = new ManagementObjectSearcher(query); 
    foreach (ManagementObject mObject in searcher.Get()) 
    { 
        Console.WriteLine((string)mObject["Name"] + "\t" + (string)mObject["FullName"]); 
    }