Solved

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

Posted on 2009-07-09
2
476 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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
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…
This video discusses moving either the default database or any database to a new volume.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now