C# program does not have take ownership privileges when UAC is disabled - c#

Basically I'm writing a tool for my job that replaces a broken file copied incorrectly by a software installation with a working version of the file from another computer.
Part of this tool requires me to take ownership of the folder containing the target file (by default this folder is owned by SYSTEM). I am an admin so I can do this through Explorer with no problem, but the tool that I created is unable to take ownership from SYSTEM, but can take ownership without error if the owner is anyone else (another admin for example)
I already have built a manifest which uses requireAdministrator, and this tool is able to take ownership from SYSTEM on my home computer, but not on my work computer.
I believe this is because UAC is disabled via group policy. Is there any way for me to run this program with admin privileges? Thanks for the help.

Related

Determine if process started as admin is already running in C#

I've been trying to determine if process A, that I installed, is already running on the machine. Process A requires admin priviliges, it is installed in C:\ProgramData and it is usually started as a service. There are many processes on the machine, that are named A. To find out, if this is my process, I inspected the path to exe file.
You can do this with process.MainModule.FileName or more elaborate setup that is documented in this question
Unfortunately, you cannot inspect the path of executable started as administrator without having administrator priviliges your self.
Is there another way to determine if the process with the name A is indeed the one you are looking for? What else can be checked beside path to executable? What workaround do you recommend?
UPDATE:
I misinterpreted the observations. If the process is started as a service, it is run as a SYSTEM user. Because of that, you cannot determine the executable path without having the admin priviliges your self.

Custom ClickOnce installation path

Is there a way to set the ClickOnce client installation folder to something static like "...\%User%\Appdata\Local\%MyProgram%?
Detailed info:
I am using ClickOnce to distribute an intranet application. Everything works fine, and the application installs and updates like it should. The one issue I am running into is one of the modules in the application moves files from one folder to another folder. The end user has Trend Micro installed, and the application was getting nuked and quarantined every time the module ran. I had the IT department whitelist the application, but due to the random installation path below "...\AppData\Local\Apps\2.0...", and because Trend Micro can't handle the '.' in 2.0, they were having to whitelist the executable below the "\Local\Apps\" folder, which they weren't comfortable doing. Is there a way to change the ClickOnce installation folder to something like "...\%User%\Appdata\Local\%MyProgram%?
Simple answer: no you cannot change that path, it is a key part of the sandboxing that is a feature of ClickOnce.
Even with custom manipulation and re-signing of the ClickOnce manifests you cannot alter that path (this is one of the ways that ClickOnce allows you to have multiple published instances installed on your machine even if they are the same version).
I'd suggest that before packaging (i.e. as part of your release build process) you sign your binaries with a code signing certificate from Verisign or similar - anti virus products typically use this as an indicator of whether something can be trusted. If that fails to solve the problem then look at whether you can whitelist based on the file name rather than the file path.

Issue Related to Read only DB file after installation

I have an Application in WinForms,
I have created SetupDevelopment project and then install an application using Setup.exe file
i have added MSAcceess db file into application folder file,
when i install an application the database file gets readonly. how can i make file writtable after installation?
I found some solution on internet and found that when i tried to install application in different path other than "C:\ProgramFile\" and make application available to all user,it solves my Problem, but i want to install an application only in "C:\ProgramFile\" and want to available to only "Current user"
how can i achieve my Problem ?
In modern Windows Operating System the folders 'Program Files' or 'Program Files (x86)' are read only for the common users. This choice has been done for security reasons and you cannot easily bypass this rule. The reccomended folders, to use just for the current user, are C:\users\username\AppData\Roaming or C:\users\username\AppData\Local identified by the Environment.SpecialFolder.ApplicationData or Environment.SpecialFolder.LocalApplicationData. To this base folders add another subfolder identifying your application or company and install your database there.
If you want your database available to all users of the current PC, you could install it in the Environment.SpecialFolder.CommonApplicationData that resolves to C:\programdata. (Again, adding a subfolder specific for your application or company)
Again, don't try to force the operating system to work against its rules. You will find yourself in trouble very often.

Create a Setup File Windows.Net C#

I have created a windows application setup program, it needs to have a text file in the application folder. The file is also included while creating the setup.
Once the setup successfully completes and my program tries to modify the file based on user input, its simple throwing an exception.
I am using Windows 7 Home Premium OS.
Any suggestion/help will be great to overcome this issue.
This is normal on a Vista or Win7 machine. Or a properly secured XP machine for that matter. The normal install location for programs, like c:\program files\your company\your app, is read only for most users. UAC is a counter-measure to malware messing with programs.
You'll need to store the text file into a writable location, the AppData folder. In the Setup project, right-click "File system on target machine" and select User's Application Data Folder. Find that file back at runtime through Environment.GetFolderPath, passing Environment.SpecialFolder.ApplicationData. Or use "User's Personal Data Folder" if the user should be able to find it back easily through the Documents folder.
What exception is being thrown? It could be a UAC issue.

Self-Updating .NET client application which needs to write in the Program File folder

Similar to: Request Windows Vista UAC elevation if path is protected?
I have a .NET Client Application installed in c:\Program Files (Windows Vista). This application should update itself, but it doesn't because of permission issues. The auto-updater should simply replace a couple of assemblies, but they are all located under c:\Program File and the application throws the following exception:
System.UnauthorizedAccessException:
Access to the path 'C:\Program
Files...' is denied.
I have no control on where the application could be installed and the permission. Is there any workaround for this? Is it possible to request the Administrator rights for a couple of seconds? Is it possible to pop a UAC window? I am pretty sure that there a workaround... Otherwise, how Firefox would be able to update itself?
Thanks in advance for the help and ideas!
Could you use a Click Once deployment method? We use this for an internal application and users have no problems with permissions when we publish a new version. They are prompted to install the update when they launch the app (if a new version exists) and it installs without a hitch.
You can't elevate a process's permissions halfway through, but you can start up another separate process with higher permissions that can do the work for you.
Get your main application to put all the files / installation details into a low-permission temporary location. When you're ready, start up a smaller application whose only job is to copy over those files to the Program Files directory (and maybe restart your main application with the new updates). Mark that application as requiring the needed permission to copy to the Program Files directory or write to the registry (or whatever else is needed).

Categories