Visual Studio produced MSI Installers, Program Files, and Administrator Access - c#

I have a working program that I am ready to generate an MSI from for distribution. The default install location is C / Program Files / Foo / Bar , and that's where I'd prefer it install. However, after installing in that location, the program refuses to run unless right-clicked and "run as administrator".
I understand that the program requires administrator access because it is installed in C / Program Files (I have an alternate install path that installs it to the desktop that lacks this problem), so I am looking for a way to not have to run the program as an administrator every single time I want to use it.
I had thought that installing the .msi "as administrator" would solve the problem, but right-clicking the .msi does not provide a "run as administrator" as I expected.
Is there any way to make it so I don't need to run the program as an administrator each time, while maintaining the c / program files install directory?

Build the MSI with a setup.exe. You can then right click the EXE and run as Admin. You can't do this to the MSI because it isn't a program, it's a document ( database ) that's executed by a different program ( msiexec.exe ).

Usually this happens when the application tries to write data in a per-machine location (like Program Files or HKEY_LOCAL_MACHINE). In this case the application will work only if it's launched as an Administrator.
A solution is to use per-user locations for application data. For example Application Data (AppData) folder under the user profile folder and HKEY_CURRENT_USER.
However, if your application really needs Administrator privileges, you can use an UAC application manifest.

Related

Winform application not running after installing into program files directory

I had created a windows form application and used setup project to create installer. But after installing my application the application is not opening from the location it is installed("c:\Programs Files(86x)\"). But the application runs fine if i copy the entire installed files to another drive. Actually my application is creating some folder inside the installed directory when running. so i thought it is some problem related to windows permissions. I had set app privileges as "requireAdministrator" and still not working. Can anyone help me with a solution?
Actually my application is creating some folder inside the installed directory when running.
Don't do that!
The Program Files folder is read only to standard users, and has been since Windows 2000. And since Windows Vista even Administrator users will require elevation to get write access into this folder.
Use the All Users Application Data folder instead (Environment.SpecialFolder.CommonApplicationData).
The alternative is an advanced installer product (meaning purchasing the full version of InstallShield or similar rather than the version included with Visual Studio) which supports adding an action to your install process that both creates the folder and sets new permissions, and does this at install time. But that's really a cheat, anyway. Just use Application Data.

Publishing VisualStudio programs

Now I have a completed program, that needs to be deployed with some files. I would like to ask, if it is possible to deploy the program without the use of ClickOnce or other external tools, with the minimum amount of required files?
The program will run on a server, and will be updated locally, but since the program is installed per user, does that mean if I log in to the server with my user, a person that uses the direct path to the program will not find it? It also makes maintaining the program slightly more annoying, because when changes come, the current maintainer will have to always navigate to the xyz folder to edit one config file. How do I publish the program without ClickOnce or other tools?
Your options include:
Click once (which you don't want)
Visual Studio installer Projects
WiX Toolset
Other installers ...
Write an installer on your own (wouldn't recommend)
Just put the exe (and other needed files) in some directory and launch the program
Put the exe on a shared location and launch from there
You can redistribute as a set of files. However I would not recommend this approach. You force the person who deploys your program :
Choose location for the files
Remember this location when time to uninstall comes.
Handle updates and version management
Restore program functionality if any of the required files will get damaged.
You can use VS Setup project template, here's tutorial
Alternatively you can use free Wix installer, it has a bit of a learning curve but provides much more power to the developer.
You can do perMachine install both with VS setup project and with wix.

Create a program/installation package that needs Admin rights

I have created a program in C# with Visual Studio 2008.
I can package it as a .msi file, which installs just fine. However, when installed, for instance, in the Program Files (x86) folder, which is the default, it can't write to the .xml file that's included (using .xml for settings and small storage). It only works when running as Administrator.
My question is, how do you build your Setup and Deployment so that it gets Admin rights automatically instead of needing user compatibility setup?
Its not recommended by microsoft to place any user files in the programFiles directory, it doesn't belong there.
Instead write those files in the ProgramData folder.
EDIT: A fast search found a duplicate question and answer here

Copy/install application to Program Files automatically when it is first run

When execution an application for the first time, I want this running applicationto be copied to C:\Program Files\
Say I give this application to my friend then he executes it for the first time and this application must do the copy to C:\Program Files\
How to code it?
You are trying to create a Click Once kind of application. Check the link Click Once Deplyment
You cannot move an EXE file while it is being executed, you will either:
have code that copies the EXE to another location
IO.File.Copy()
or have an installer
Click Once installs to a really obsecure location, not program files. It does NOT require admin.
If you add a MSI setup project to your solution you can see this answer to hardcode the install path to C:\Program Files\
If you choose the MSI solution you will need Admin to install. Because the setup.exe is responsible for the downloading/installing components (such as dotnet) it requires Admin to be executed.
If you know the PC your application is going to be installed on already has the .Net Framework & etc then the easiest way to avoid the UAC prompt is to run the MSI not the setup.exe. Running the MSI wont ask for admin.
You can also follow this answer to make your upgrades install to the same folder.

Providing Directory's Full Control for Non-admin Users under Windows 7

I'm working on a software written in C# and for .NET Framework 3.5. The installer for the software is created by a "Setup and Deployment > Setup Project" from Visual Studio 2008. Originally the software was written for Windows XP. Now I need to modify it so that it also works under Windows 7.
In Windows 7, admin privilege is required for a software to be installed. So the admin becomes the owner of the installation directory created during the installation. The software will be used by non-admin users, who only have read and execute access of the installation directory by default. But the software is designed in such a way that it can write to the installation directory. There is a startup.ini configuration file resides in the installation directory which the non-admins should be able to modify to alter the behavior of the software.
If the software is run from non-admin account, it can't write to the installation directory. Also non-admins can't change the startup.ini file.
As a workaround, currently the admin installs it changes the permission of the installation directory so that non-admins can write and modify it.
I want it to be automatically done after installation. Somehow the installer should take care of the permission. How can I do that? Note that I can't change the software behavior so it no longer writes into the installation directory. The non-admins must be given necessary privilege so that they can have modify and write permission.
I'm using Windows XP SP3 for the development. All OSes involved (XP and 7) are 32 bit.
Thanks in advance.
you should change the application so that it stores its INI file at a location which normal users can write - see http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/77aaf6e1-8de5-4529-9b26-fa89b55fcc49
EDIT:
IF that is absolutley not possible then:
create a "custom action" in the setup project and change the permissions... a good starting point is
http://www.redmondpie.com/applying-permissions-on-any-windows-folder-using-c/
and
http://msdn.microsoft.com/en-us/library/system.security.accesscontrol.filesystemsecurity.addaccessrule.aspx
For the record, your program is broken on Windows XP as well. XP also requires admin rights to install this way or write to Program Files folders. The difference is that so many people have admin by default in XP.
If you can't modify the program, you will have to write a custom installer action to grant write access to the ini file in question. There is no need to grant this access to the entire folder.

Categories