C# WinForm 应用程序中的用户帐户名未更新

发布于 2024-09-24 11:46:06 字数 1560 浏览 6 评论 0原文

我正在编辑 ac# WinForm 解决方案,但我不明白获取用户帐户名的代码。代码如下所示。

该应用程序为每个用户帐户显示一个自定义表单,并且需要用户帐户名才能从 SQL 数据库获取特定于用户的配置值。

据我所知,所发生的情况是,返回的用户名对于访问的第一个用户帐户来说是正确的,但是在切换到不同的用户帐户后,返回的用户帐户名称不会更新,并且初始用户帐户名称仍然是回来了。

#region "Function to retrieve LoggedIn user"
/// <summary>
/// "Function to retrieve LoggedIn user"
/// </summary>
/// <returns></returns>
private string GetLoggedInUserName()
{
    ManagementClass objManClass = new ManagementClass("Win32_Process");
    ManagementObjectCollection arrManObjects = objManClass.GetInstances();
    foreach (ManagementObject objMan in arrManObjects)
    {
        if (objMan["Name"].ToString().Trim().ToLower() == "explorer.exe")
        {
            string[] arrArgs = { "", "" };
            try
            {
                objMan.InvokeMethod("GetOwner", arrArgs);
                sUserName = arrArgs[0];
                break;
            }
            catch (Exception lExp)
            {
                BusinessObject.Logger.Logger.Log(lExp);
            }
        }
    }
    return sUserName;
}
#endregion

这个应用程序要在 XP、Vista 和 7 上运行。

我的直觉是只使用类似...

string sUserName = Environment.UserName;

...但我对 Windows 操作系统的了解很差,编写原始代码的人比我聪明得多。

所以我的两个问题是: (1) 为什么当我更改用户帐户时,此代码似乎没有更新为新用户名? (2) 为什么使用“explore.exe”方法而不是简单地使用“Environment.UserName”?

此外,我的解决方案中的两个项目都有一个 GetLoggedInUserName() 方法。一个项目在后台运行,并带有一个调用另一个项目的计时器,该项目会生成用户自定义的表单。

我还有另一个相关问题,关于为什么除了管理员帐户之外的所有用户帐户都无法显示该表单,一旦我弄清楚这个问题,我将作为一个单独的问题发布。

I am editing a c# WinForm solution and I do not understand the code that gets the user account name. The code is shown below.

The application shows a customized form for each user account and the user account name is needed to get user-specific configuration values from an SQL database.

What happens, to the best I can tell, is the returned user name is correct for the first user account accessed, but after switching to a different user account, the returned user account name is not updated and the initial user account name continues to be returned.

#region "Function to retrieve LoggedIn user"
/// <summary>
/// "Function to retrieve LoggedIn user"
/// </summary>
/// <returns></returns>
private string GetLoggedInUserName()
{
    ManagementClass objManClass = new ManagementClass("Win32_Process");
    ManagementObjectCollection arrManObjects = objManClass.GetInstances();
    foreach (ManagementObject objMan in arrManObjects)
    {
        if (objMan["Name"].ToString().Trim().ToLower() == "explorer.exe")
        {
            string[] arrArgs = { "", "" };
            try
            {
                objMan.InvokeMethod("GetOwner", arrArgs);
                sUserName = arrArgs[0];
                break;
            }
            catch (Exception lExp)
            {
                BusinessObject.Logger.Logger.Log(lExp);
            }
        }
    }
    return sUserName;
}
#endregion

This application is to run on XP, Vista and 7.

My instinct is to just use something like...

string sUserName = Environment.UserName;

...but my knowledge of the Windows OS is poor and the people who wrote the original code are much smarter than me.

So my two questions are:
(1) Why does this code appear to not update to the new user name when I change user accounts?
(2) why use the 'explore.exe' method instead of simply using 'Environment.UserName'?

Also, two projects in my solution have a GetLoggedInUserName()method. One project runs in the background with a timer that calls the other project, and that project generates the user-customized form.

I have another related question about why the form fails to appear for all user accounts except the admin account that I will post as a separate question once I figure out this question.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

末が日狂欢 2024-10-01 11:46:06

如果您想要当前登录的用户,可以使用 WindowsIdentity 对象:

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

If you want the currently logged in user, use can use the WindowsIdentity object:

string currentUser = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
半城柳色半声笛 2024-10-01 11:46:06

当您登录到 Windows 机器时,Explorer 进程始终在运行,因此总能找到它。如果您打开任务管理器并查看进程,您将看到它以及启动它的帐户。它看起来像是回到了 VBScript,尽管我确信还有一种更简单的方法。

没有充分的理由使用 WMI 来获取本地计算机上的当前用户帐户而不是其他更简单的方法。

The Explorer process is always running when you log onto a Windows box, so it will always be found. If you open Task Manager and view the processes you will see it, and the account that started it. It looks like a throw back to VBScript, although I'm sure that there is an easier way to it with that too.

There is no good reason to use WMI to get the current user account on a local machine over other simpler methods.

痴梦一场 2024-10-01 11:46:06

对于用户名位尝试...

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

For the user name bit try ...

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文