I've been programming in C# for quite sometime now. I've got lot of help from you'll during the past few days. But I'm stuck here...
I have built an .exe file and changed the manifest so that on execution it needs administrative privileges. I changed the following in the manifest
requestedExecutionLevel level="requireAdministrator" uiAccess="false"
This would prompt a UAC window which would inform the user that the .exe file requires admin privileges.
Now, I've made my .exe file to copy itself in another location on the local machine. But the catch is, the copied file also requires admin privileges when I run it. I want to edit the privileges of that copied file. I want the copied file to have no such constraint of admin privileges. I want to make it run normally.
I need this done because I've made the .exe file to change the registry which calls the copied .exe file at every startup. I've noticed that with the copied .exe file having admin privileges, the registry tweak doesn't work. Although it works perfectly when the .exe file doesn't require any admin privileges.
Is there a way out of this?
Related
Access to path denied error is encountered in Program files (x86),
while working with a DLL that is got from nuget package: AODL for reading ODF files - https://www.nuget.org/packages/AODL/ after I created a MSI file using SETUP Project
In the code, I don't suspect the file creation part for I create this file in the user chosen file conversion path but NOT IN PROGRAM FILES folder path:
File.WriteAllText(targetFileName, sb.ToString(), Encoding.UTF8);
That's why I simply suspect the DLL, please let me know how I could find the error and fix this.
A bunch of directories - both Programm Files, the root directory of the System Drive, Windows - are heavily protected by NTFS rights. Writing them is usually a plain "no-go". Unless you run around with full administrative rights - wich only Instalers and very rare Adminsitrative tools should even consider - you will not be able to write there.
However you indicated this happens on a read. Reads being blocked like that is very unusual. You need to check what rights are set on those folders and why. Maybe the installer accidentally copied the rights from your computer, wich only makes sense with your users and groups? Maybe Windows or a third party broke those rights? Not a lot of options I can think of that could apply here.
For this application, even for reading the DLLs from the Program Files folder I needed Admin rights so I forced the application to have such rights for execution.
The below line for the newly created application manifest file is changed and that solved the issue.
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
The fix is explained well in How do I force my .NET application to run as administrator?
The reasons are stated well in https://stackoverflow.com/a/50588465/129130
I searched about this on internet but could not find exact answer.
In my application, I have a functionality which copies the files in folder (say pqr) to the location where user wanted. The location of source folder is the location of installed application (say c:\Program Files\abc\pqr).
When user is logged in to the machine with 'administrator rights', user is able to use this functionality. But when the user is logged in to the machine with having 'User rights' (non-admin user), this functionality throws exception that access to c:\Program Files\abc\pqr folder is denied.
I tried to elevate user privileges by using below attributes to copy method:
[PrincipalPermission(SecurityAction.Demand, Role = #"BUILTIN\Administrators")]
[PermissionSet(SecurityAction.Demand, Name="FullTrust")]
I also tried adding manifest file with below changes:
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
But none of the approach worked.
Then I installed my application on D drive. And after that when I tried with the non-admin user the functionality worked as expected.
So when the application is installed in C:\Program Files, functionality was not working for non-admin user.But after installing the application to another location it worked for non-admin user.
So my question is, is it possible to give the rights if C:\Program Files to non-admin user programmatically in the application or we need to have admin users for using this functionality?
Changing the manifest is most simple way to elevate privileges. Change it in VS. But it is bad practice to require admin privileges for regular software.
You can make program folder accessible for all users during install. For example it can be made with InnoSetup with Permissions: users-modify in [Files] group.
If the source files are in Program Files and you copy in another location, probably the problem is in wrong file access when opening source files. Open for reading only.
var startInfo = new ProcessStartInfo("yourApplication.exe") { Verb = "runas" };
Process.Start(startInfo);
Environment.Exit(0);
or
application.exit();
if you are using winforms
but be careful to excute this code one time because if you put it in the load event you will get overflow in memory
My WPF application writes an XML file to a folder within the CommonApplicationData folder on a Windows 7/64 machine. This works fine from Visual Studio 2010. When running from the .EXE file, I get a System.UnauthorizedAccessException when writing the file.
Is this a problem with my initial setup of the folder? Or is this related to the permissions of the executable file itself? Not quite sure how to handle this one??
Paul
I think that it is a problem with permissions to the folder.
Probably Visual Studio runs your application as an administrator and the .EXE file is executed as a normal user.
Perhaps you want to re-evaluate storing that data (XML) in that location all the time. Limited users won't be able to write to it. Sure, you can force admin privs but your users may not always have that option (and it's kinda a hack anyway).
The question below seems to outline a work-around depending on the user's priv level.
writing files to Common Application Data folder denied
Right click on the *.exe file and "Run as administrator".
I have a small C# program to modify a xml file which is located under Program Files. The machine is a Windows 7 machine. This small program is launched by a batch file (called A.bat) because I want to pass a parameter to it.
I have a master batch file (called M.bat) which is the start point. The M.bat will start an installer and wait untile the installation finished. Then the M.bat will start A.bat which will launch my small program with a parameter.
Right now I get the following exception:
System.UnauthorizedAccessException: Access to the path 'C:\Program Files\MyTest\Test.config' is denied.
I know it is caused by tighter security in Win7. It works fine under XP.
I cannot use "Run as Administrator" to start M.bat or manually "Run as Administrator" to start A.bat because both will report cannot find the target executable (because the start location is not really the "current" location then).
Is there a way to start an executable as Administrator in batch file? or in C# program I can give myself Administrator right?
or ...
Not in a way that is invisible/hidden from the user... I would suggest finding a way to make it work when run as an administrator. Or you could set the application manifest (see this: http://www.enusbaum.com/blog/2007/08/26/how-to-run-your-c-application-as-administrator-in-windows-vista/) to run your app as admin, that might work as well.
Bottom line, you cannot run with admin privileges unless you run as admin, or unless your user switches off UAC (which is not recommended at all).
You'll need to elevated your privileges through a UAC prompt. Add a manifest to your program as described in this answer.
You should request for admin privileges at program start up. Look at this sample
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).