Create a program/installation package that needs Admin rights - c#

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

Related

C# Winform Application Only Working as Run as Administrator

I am working on a application which is Windows Form Application and performs the CRUD operations in Microsoft Access Database (.mdb).
Old application :
The application was developed in C# with Microsoft .Net Framework 4.0.
The previous developers used Visual Studio installer projects to create the setup file.
The default installation path is "C:\Program Files (x86)\MyProduct".
This folder will have the MyProduct.exe executable and dependent dlls and also and Microsoft Access database file (.mdb). The shortcut will be created on desktop. When you run the MyProduct.exe it can access the database without any errors and can perform the CRUD operations.
New Application:
Now I have upgraded the .Net framework to 4.5.2 and used Installshield to create the installer. Installation path will be same "C:\Program Files (x86)\MyProduct".
Now the problem is, when I execute the exe and do any insert/update/delete operation it throws the error that "operation must use an updateable query". This means the database is not accessible. I tried running the exe as an administrator user and then only it is working fine.
My operating system is Window 10.
Note : I modified the database.mdb properties and provided FullControl to all the users for both new and old versions. But new version is not working without the admin user.
For the newer version I changed the installation path to "C:\My Folder\MyProduct" and it is working without any error. Only C:\Program Files is giving the error. The strange thing is older version is also installed in Program Files but it does not throw any errors.
Other difference between old and new application is, Older version uses Microsoft JET engine driver to use Microsoft Access database, while newer version uses Microsoft ACE OLEDB driver to use Microsoft Access Database.
I am not sure why this is happening. Older version is working but newer is not.
Can any one help me with this?
I would move the install to %userappdata% or some folder that does not require elevated rights. While you can place the .exe in program files, ANY data file in those locations tends to have VERY limited rights these days (they continue to lock down windows more and more).
Also, be carefull. Even if you say installed to my docuemnts? If you REQUIRE elevated rights during that install, then of then often DURING the install then the files and folder you create ALSO will have elevated rights.
So, don't place ANY data files or files that your code has to read in program files. These days such system folders tend to be restriced and have locked down rights.
%appdata% is your best bet. That will in most cases translate to:
C:\Users\AlbertKallal\AppData\Roaming
This ALSO means that your code better not have hard coded path names either. (you get/grab your path from the executing assembly. If you have a lot of code, and paths are hard coded? Then you can bite the bullet and say install to c:\MyCoolApp or some folder you create in the c:\ drive. However, with so many people doing remote work, then even c:\SomeFolder can be a bad choice, since on terminal services, all users share the same c:\ drive.
So, move the install out of program files, or at the very least move out the data file parts to a known read/write folder that all users by default will have rights to, and not require elevated rights to use such folders. So, be very aware of any data file parts you have as part of your install. As noted %appdata% is a good choice, but then keep in mind that the path names to the executable and the data file location will be dynamic (the users name will be part of the path name). And if you don't think you ever support terminal services or remote desktop? Then you can use/risk a hard coded c:\MyApp folder approach here.
At the end of the day, you REALLY need to avoid elevated rights DURING the install, and this is especially the case if data files are part of the install - since those files will ALSO inherit the elevated rights during the install. And for sure using program file which was common 10 or 15 years ago for programs AND data? Well the program part is still ok, but any data files now can't for all practical purposes be placed in program files.

How to disable recovering missed files for Visual Studio Installer Projects?

I created a program that uses images from the Blizzard API service.
I created an installer for the program, and I place the images in the "User's Application Data Folder".
Installation is excellent, the images are unpacked into the folder "AppData / Roaming / MyApp".
There is a need for the images to be deleted, and the program will download them from the Blizzard service itself.
But when you delete the installed files, the installer starts restoring the files, and the program cannot download fresh data from the Blizzard service.
Is it possible to disable recovery for the folder "AppData / Roaming / MyApp"?
Or you have any idea how to resolve it?
For creating installer I using "Microsoft Visual Studio Installer Projects" https://marketplace.visualstudio.com/items?itemName=VisualStudioClient.MicrosoftVisualStudio2017InstallerProjects
Help me please.
Download Files: Can you install your service and then download the files from the server and not install them via MSI at all? This
will de-couple them from any MSI self-repair interference. MSI does not know anything about the files in this scenario.
Self-Repair: Windows Installer (MSI) self-repair is a very intricate thing to deal with: Why does the MSI installer reconfigure if I delete a file?. And here is a more elaborate piece: How do I avoid triggering MSI self-repair with my WiX / MSI package? I wouldn't really recommend studying it too much: the concept is simple (keep installed files in correct version), but the problems that result are intricate and difficult to fix.
Blank GUIDs: There is a method where you can install the files via components that has a blank GUID. Then they are simply installed and never checked again. This should eliminate self-repair problems.
Read-Only Templates: Finally you can install a bunch of template files to a per-machine path (for example under program files) and then copy them in place for each user. Can be very effective. You can then overwrite them and delete at will.
Recommendation: I would install my service and download the files from the server on demand. That should give you full control of the whole process from code that is easier to debug than setup code (custom actions).

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.

Best way to deploy Visual Studio application that can run without installing

I wrote a fairly simple application with C#/.NET and can't figure out a good way to publish it. It's a sort of a "tool" that users would only run once, or run every few months. Because of this, I'm hoping that there is a way I could deploy it where it wouldn't need installing to run (it could just be run by double-clicking an EXE file straight after downloading).
However, it still needs (somehow) to include the correct version of .NET, libraries, etc. so it will run correctly. I know this is included when using ClickOnce, but that still installs the application onto the user's computer.
Is there a way this can be done?
EDIT - \bin\Debug
myAppName.application
myAppName.exe
myAppName.exe.config
myAppName.exe.manifest
myAppName.pdb
myAppName.vshost.application
myAppName.vshost.exe
myAppName.vshost.exe.config
myAppName.vshost.exe.manifest
extraLibrary.dll
as well as two folders
app.publish
Resources
It is possible and is deceptively easy:
"Publish" the application (to, say, some folder on drive C), either from menu Build or from the project's properties → Publish. This will create an installer for a ClickOnce application.
But instead of using the produced installer, find the produced files (the EXE file and the .config, .manifest, and .application files, along with any DLL files, etc.) - they are all in the same folder and typically in the bin\Debug folder below the project file (.csproj).
Zip that folder (leave out any *.vhost.* files and the app.publish folder (they are not needed), and the .pdb files unless you foresee debugging directly on your user's system (for example, by remote control)), and provide it to the users.
An added advantage is that, as a ClickOnce application, it does not require administrative privileges to run (if your application follows the normal guidelines for which folders to use for application data, etc.).
As for .NET, you can check for the minimum required version of .NET being installed (or at all) in the application (most users will already have it installed) and present a dialog with a link to the download page on the Microsoft website (or point to one of your pages that could redirect to the Microsoft page - this makes it more robust if the Microsoft URL change). As it is a small utility, you could target .NET 2.0 to reduce a user's probability to install .NET.
It works. We use this method during development and test to avoid constantly uninstalling and installing the application and still being quite close to how the final application will run.
First, you need to publish the file by:
BUILD -> PUBLISH or by right clicking project on Solution Explorer -> properties -> publish or select project in Solution Explorer and press Alt + Enter
NOTE: if you are using Visual Studio 2013 then in properties you have to go to BUILD and then you have to disable define DEBUG constant and define TRACE constant and you are ready to go.
Save your file to a particular folder.
Find the produced files (the EXE file and the .config, .manifest, and .application files, along with any DLL files, etc.) - they are all in the same folder and type in the bin\Debug folder below the project file (.csproj).
In Visual Studio they are in the Application Files folder and inside that, you just need the .exe and dll files.
(You have to delete ClickOnce and other files and then make this folder a zip file and distribute it.)
NOTE: The ClickOnce application does install the project to the system, but it has one advantage. You DO NOT require administrative privileges here to run (if your application follows the normal guidelines for which folders to use for application data, etc.).

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.

Categories