Solved

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

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

ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Error in script 11 58
Wav problem 4 27
rest webservice call over https via c# 6 42
VB: Convert 2 dates to specific format 24 48
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

803 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