Install an application for all users
In this article I present techniques based on Visual Studio 2005 Deployment, C#, Windows Registry.
I have recently encountered one of those errors you should expect but you don’t. I have an application for which I have created an installer project, under Visual Studio 2005. The application works fine after installation for the administrative role user that installed it, but when switching to a less priviledge user on the same computer the install/ repair program starts for that user prompting for the installation kit path. As an aditional detail, the setup kit creates the application registry keys it requires under HKEY_CURRENT_USER key. And this is the flaw…
When creating registry entries for an application that might be used by more than one user, each of which having different access rights on the computer, we should place the registry key under the “user/ machine hive” node in the Registry view of the deployment project. This way we ensure that the keys get into the right place when the application is first installed. According to Microsoft’s definition, the keys placed under user/ machine hive will be placed either under HKEY_CURRENT_USER when the application is insalled only for one user and under HKEY_LOCAL_MACHINE when the application is intalled for all users. Here is a screenshot :
The problem with keys under HKEY_LOCAL_MACHINE is that they are read only for restricted users, for example user belonging only to the “Users” group. It is though required to assign special permissions for those keys a user not only to read but also modify. The reason I needed this was to save some user settings in the registry, so when a less privileged user saves the settings, he/ she must also have proper permisions.
To solve my problem I’ve created a custom installer class implemented an event handler for the Commited event. In this handler, write access permissions are given to users belonging to the “Users” group.
I give bellow the implementation of my event handler.
// Event handler for ‘Committed’ event.
private void CustomInstaller_Committed( object sender, InstallEventArgs e ){
RegistryKey hkCustom = null;RegistryKey hkTester = null;
// depending on the installation options, we will search for the application’s registry keys :
// 1. – if installation is performed just for the current user, the keys will be under HKEY_CURRENT_USER
// 2. – else if the installation is performed for all users, the keys will be under HKEY_LOCAL_MACHINE
hkCustom = Registry.CurrentUser.OpenSubKey( “Software” ).OpenSubKey( “CustomInstallerTester” );if( hkCustom != null ){
hkTester = hkCustom.OpenSubKey( “Tester99″, RegistryKeyPermissionCheck.ReadWriteSubTree /*required*/ );}
if( hkTester == null ){
hkCustom = Registry.LocalMachine.OpenSubKey( “Software” ).OpenSubKey( “CustomInstallerTester” );
}
if( hkCustom != null ){
if( hkTester == null ){
hkTester = hkCustom.OpenSubKey( “Tester99″, RegistryKeyPermissionCheck.ReadWriteSubTree /*required*/ );}}
if( hkTester != null ){
System.Security.AccessControl.RegistrySecurity regSec = new System.Security.AccessControl.RegistrySecurity( );// grant write permissions to the users group :
regSec.AddAccessRule( new RegistryAccessRule( “users”,
RegistryRights.ReadKey | RegistryRights.WriteKey | RegistryRights.Delete,
InheritanceFlags.ContainerInherit,PropagationFlags.None,
AccessControlType.Allow) );hkTester.SetAccessControl( regSec );}
if( hkTester != null ) { hkTester.Close( ); }
if( hkCustom != null ) { hkCustom.Close( ); }
}
5 Comments »
Leave a comment
-
Archives
- May 2009 (1)
- April 2009 (4)
- February 2008 (1)
- November 2007 (1)
- August 2007 (1)
- July 2007 (2)
- June 2007 (2)
- May 2007 (1)
-
Categories
-
RSS
Entries RSS
Comments RSS

Sounds like an excellent solution. Could you please give away an example project, class and setup as you suggest?
I would really appreciate it. Thanxs in advance for any reply,
Erik
I can, if you provide an e-mail address such that I can send you an archive… Mine is cosmin1595@yahoo.com.
Stefan says : I absolutely agree with this !
Somehow i missed the point. Probably lost in translation
Anyway … nice blog to visit.
cheers, Torbay.
Hello, I can’t get the drift how to tot up your blog in my rss reader
By.