views:

226

answers:

3

Hello,

I need to get the username of the user running the installer for my custom install action. Because the installer gets special priviledges, Environment.UserName just gives "SYSTEM".

Environment.SpecialFolders.ApplicationData returns the current users appdata folder, but I can't reliably dig the username out of it.

More case specifics:

Using a Visual Studio 2008 Setup Project The custom action is an installer class run after install and is the only one in the project.

+1  A: 

Use WindowsIdentity.GetCurrent().Name from the System.Security.Principal namespace. This will include the domain portion as well so if you don't need that add a split to the end of it. WindowsIdentity.GetCurrent().Name.Split('')[1].

using System.Security.Principal;

this.nametext = WindowsIdentity.GetCurrent().Name.Split('')[1];
Scott Boettger
There were other answers that suggested this too, but seem to have been deleted.This doesn't return the users name in vista, it still returns SYSTEM
Septih
take a look at this post. http://stackoverflow.com/questions/400872/c-active-directory-check-username-password. It's more about validating but it might be useful.
Scott Boettger
A: 

First, make sure you have the Impersonate bit set to OFF. Here is how.

Unfortunately, there is not a way to directly set this flag for a custom action in the UI for the setup project in the Visual Studio IDE. In Visual Studio 2005, you can use a post-build step that modifies the MSI to set this bit using a strategy previously described in this blog post.

Second, I assume you are running Vista because people seem to have this problem with Vista and someone asked this question on MSDN and his solution was to follow the blog post linked here.

Robert Flaming's Blog post on UAC in MSI Notes: The NoImpersonate Bit Mistake also gives some insight into these issues.

0A0D
Right, I already have the Impersonate bit turned off. I looked at the blog which answered the msdn question and tried the manifest solution, but that doesn't seem to have changed anything. I basically just copied the manifest from the blog and saved it as setup.exe.manifest
Septih
Sorry but I dont have anymore info atm
0A0D
A: 

I've never touched VS setup projects (played with WiX though and can recommend it). Looking at your problem it seems that your CA runs deferred (and with elevated privileges).

Searching the net for VS setup projects I came across a lengthy article [1] that contained this paragraph (search for "deferred"):

In other words, the Visual Studio design restricts you to custom actions that are called when your files are on the system (deferred custom actions), which means that you must use the CustomActionData property. Other tools that generate MSI files are often more flexible, so if you anticipate complex setups, investigate those tools.

1: http://www.simple-talk.com/dotnet/visual-studio/visual-studio-setup---projects-and-custom-actions/

Benjamin Podszun