Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1100995
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T00:58:09+00:00 2026-05-17T00:58:09+00:00

I am editing a c# WinForm solution and I do not understand the code

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T00:58:09+00:00Added an answer on May 17, 2026 at 12:58 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have several controls in a winform application that I added databindings to using
Editing my nib for a document-based Cocoa application, I see that when I have
Editing this code here on Stackoverflow and I'm really near to get the result
When editing data in a JTable (Nimbus L & F), as the user tabs
When editing Silverlight XAML in Visual Studio 2008, the Document Outline panel is not
When editing a resource in the Visual Studio resource view, I find that a
I am using Winform And C#. In that i added calander cell in datagridview
I have an application for drawing and editing vector graphics in WinForms I have
When editing C# code in Visual Studio 2010, the ENTER key makes IntelliSense complete
I'm refactoring a WinForms (.NET 4) application that makes use of a TabControl to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.