Solved

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

Posted on 2009-07-09
2
486 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

717 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