Solved

System.Environment.UserName in a .net setup project gives me SYSTEM

Posted on 2009-07-09
2
485 Views
Last Modified: 2012-06-21
Hi,
when starting my application from autostart or by an icon-click System.Environment.UserName gives me the logged in user's name. I want this.

When starting from a .net setup project (on finish) System.Environment.UserName gives me "SYSTEM". I don't want this.

Grateful for an answer!
0
Comment
Question by:Jonasxx
2 Comments
 
LVL 21

Assisted Solution

by:Craig Wagner
Craig Wagner earned 20 total points
ID: 24841351
I know that MSIExec does some of its work under a LocalSystem process, so that's probably what's happening. Have you tried using WindowsIdentity.GetCurrent().Name instead? It might give you the same result, but it's worth a try.
0
 

Accepted Solution

by:
Jonasxx earned 0 total points
ID: 24877440
WindowsIdentity.GetCurrent().Name is the same. I guess a part of the problem is that there can be many user logged in at the same time, solved it by in System.Management.
if (System.Security.Principal.WindowsIdentity.GetCurrent().IsSystem)
                    {
                        ConnectionOptions co = new ConnectionOptions();
                        ManagementScope ms = new ManagementScope(@"\\localhost\root\cimv2", co);
                        ObjectQuery oq = new ObjectQuery("SELECT * FROM Win32_ComputerSystem");
                        ManagementObjectSearcher searcher = new ManagementObjectSearcher(ms, oq);
                        ManagementObjectCollection collection = searcher.Get();
                        foreach (ManagementObject mo in collection)
                        {
                            userName = mo["UserName"].ToString();
                            int position = userName.LastIndexOf(@"\");
                            if (position != -1)
                            {
                                userName = userName.Substring(++position);
                            }
                            break;
                        }
                    }
                }

Open in new window

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question