System.UnauthorizedAccessException: Access to the path "..." is denied
Solution 1
When installing an application the installer usually asks for administrative privileges. If the user chooses "Yes" the program will run and have read and write access to a larger variety of paths than what a normal user has. If the case is such that the installer did not ask for administrative privileges, it might just be that ClickOnce automatically runs under some sort of elevated privileges.
I'd suggest you write to the local appdata folder instead, but if you feel you really want to write to the very same directory as your application you must first run your app with administrator privileges.
To make your application always ask for administrator privileges you can modify your app's manifest file and set the requestedExecutionLevel
tag's level
attribute to requireAdministrator
:
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
You can read a bit more in How do I force my .NET application to run as administrator?
Solution 2
I was running a program that would generate file. The destination folder was read only. And it would crash with the error. Removing the read-only attribute using folder properties resolved the error.
Solution 3
First, if you need to write any data you should use the Environment.SpecialFolder
enumeration.
Second, do not write to any folder where the application is deployed because it is usually read only for applications. You probably want to write to the ApplicationData
or LocalApplicationData
enumerations.
Solution 4
I think that access to %appdata% is restricted by default on windows 8 (or 7) onwards. When the app installed via ClickOnce you are probably prompted to give it permission to alter this computer - is that right?
You can try running the app with admin permissions as a test (hold shift, right click the .exe, run as administrator) which will probably solve it, but it's not an ideal way to do it.
Instead try another folder, something like:
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)
or
Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments )
which should give you better luck.
As a side note - if you are building paths in code, rather than using
path + "\\" + path + "\\" + filename
which is prone to failure (path may already have a \ on the end) it is usually better to use Path.Combine(..)
String destinationPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), fileName);
Related videos on Youtube

user5313398
Updated on July 26, 2022Comments
-
user5313398 3 months
I have C# wpf installation done with .net using click once installation. All works fine. Then I have the following code which is part of the installed program:
String destinationPath = System.Windows.Forms.Application.StartupPath + "\\" + fileName; File.Copy(path, destinationPath, true); this.DialogResult = true; this.Close();
But I get this error:
System.UnauthorizedAccessException: Access to the path C:\user\pc\appdata\local\apps\2.0....... is denied.
at System.IO.File.InternalCopy(String sourceFileName, String destFileName, Boolean overwrite, Boolean checkHost) at System.IO.File.Copy(String sourceFileName, String destFileName, Boolean overwrite)
Is it a permission error or do I need to tweak something in my code?
What puzzles me is why the user is able to install the program using click once into that directory without any issues, but uploading a file to it doesn't work?
-
Dour High ArchStop trying to copy files to your application folder, that folder can be replaced if the user repairs or reinstalls your app. Use
ApplicationData
instead.
-
-
user5313398 about 6 yearsI need to load the license file into where the application is installed? What best way I can do that then ?
-
user5313398 about 6 years@jb I need to load the license file into where the application is installed? What best way I can do that then ?
-
Justin Loveless about 6 years@user5313398 It is not possible unless the user is able to have elevated permissions
-
user5313398 about 6 years@VisualVicent the very thing I choose Clickonce because it does not need the extra permission settings right? It works on many other pc well except for this particular one its giving me issue. Ok I followed your link and currently I added the new manifest file and current settings is this <requestedExecutionLevel level="asInvoker" uiAccess="false" /> so I just change this to <requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
-
Visual Vincent about 6 years@user5313398 :
the very thing I choose Clickonce because it does not need the extra permission settings right?
- I'm not very familiar with ClickOnce, but your application will most likely not run under the same privileges as the ClickOnce installer. That would be idiotic by Microsoft because who knows what privileges you want for your app? -
Visual Vincent about 6 years@user5313398 : Yes, you just change the already existing
requestedExecutionLevel
tag. -
user5313398 about 6 yearsSo what is your current best suggestion to uninstall it first?
-
Justin Loveless about 6 yearsIt could be a number of issues. Maybe UAC has been disabled on other machines or different Group Policy settings to name a couple. Maybe other users were able to run the app as an administrator and have been doing so.
-
Visual Vincent about 6 years@user5313398 : According to this MSDN article about ClickOnce, the ClickOnce system automatically grants only the required security permissions for your app. I would suspect than this does not include full administrator rights (unless explicitly stated), as that could be exploited.
-
Visual Vincent about 6 years@user5313398 : I don't know, as I said I am not very familiar with ClickOnce. Can't you just re-publish it and then re-install/update it?
-
Justin Loveless about 6 years@VisualVincent By default ClickOnce does not include administrator privileges, and there are a special set of permissions that are used when deploying an application that is specific to the type of deployment and where it is being deployed from (which is why the app can install to a directory, but when you run the application you cannot write to that install directory).
-
Justin Loveless about 6 yearsAlso, it is bad practice to require administrative permissions for your application. If you find yourself in a position where you need to require it, you may want to rethink what you're doing first and see if you can re-architect your solution in a way to avoid it. As @user5313398 said, you used ClickOnce because you do not need a special set of permissions. Requiring administrative permissions in an enterprise environment is a nightmare waiting to happen.
-
user5313398 about 6 years@JustinLoveless so what is your suggestion for my case? I need to upload the file into same folder where its installed?
-
user5313398 about 6 years@JustinLoveless I am surprised why it worked in other pc well. So what is your best suggestion
-
Visual Vincent about 6 years@JustinLoveless : Well aware of that, but if he needs it to be in the startup directory then it's either that or using a normal installer (which will require administrative rights anyway). -- Though, he could always skip the installer and ship it all in a .zip file.
-
user5313398 about 6 years@So what is best suggestion then?
-
Visual Vincent about 6 years@user5313398 : See if this article might help you: How to: Include a Data File in a ClickOnce Application.
-
Justin Loveless about 6 years@VisualVincent from what I understand he isn't having an issue with installing, but the code he posted is trying to run after installation has completed, but correct me if I am wrong there. If you absolutely need to write the file to that protected directory, you either have to require administrative permissions on your application, or change the permissions on the folder to allow that user's group to have access.
-
user5313398 about 6 years@JustinLoveless yes you are absolutely right I have no issue installating it . I had installed well. Is the next step for me to load the license file into same folder with where it has been installed. I cant change the permission manually? I prefer where I can change some settings on my application or during installation.
-
user5313398 about 6 years@VisualVincent I have read before this link on the include data file in a clickonce the challenge is that my file will change for every different client so that is not practical for my case.
-
Justin Loveless about 6 years@user5313398 There is nothing you can do during installation to allow you to write to that folder post installation. The only thing you change in your application is to require administrative privileges. The way UAC works is that by default all users (even users with admin privileges) only have user level permissions, unless they elevate and are granted an admin token. Elevated permissions MUST be granted before an application is run via the UAC prompt. Unless you launch another process from your app that prompts UAC, you cant change settings mid execution to allow you to write to that folder.
-
Justin Loveless about 6 years@user5313398 don't write to that folder
-
user5313398 about 6 years@JustinLoveless so will this help in any case <requestedExecutionLevel level="requireAdministrator" uiAccess="false" />?
-
user5313398 about 6 yearsthe problem is that the license has to be where the .exe file is .
-
Visual Vincent about 6 years@user5313398 : It will help if the user has the authority to run the app with administrator privileges.
-
Visual Vincent about 6 years@JustinLoveless :
from what I understand he isn't having an issue with installing
- I never said his problem had to do with the installation either, but if he cannot publish a ClickOnce app with therequireAdministrator
level he must switch to a normal installer (which requires admin privileges anyway) or just place everything in a .zip file. -
Visual Vincent about 6 years@JustinLoveless : It's either 1) Always run the app as admin, or 2) Install the app using a normal installer with custom actions to create the file, or 3) Place everything in a .zip file.
-
Visual Vincent about 6 yearsEither installation-wise way you do it @user5313398 you will almost always require administrator privileges to write to the same folder as the app (unless the user chooses to install it elsewhere).
-
user5313398 about 6 years@VisualVincent when you say installation-wise way is just adding the manifest file or anything extra? So in conclusion meaning now if he once to install using Cliconce then he must have admin privellege can I say that in short?
-
user5313398 about 6 yearsWhat you mean place everything in a .zip file?
-
Visual Vincent about 6 years@user5313398 : When I say "installation-wise way" I mean any way where you utilize an installer. And yes, if you set
requireAdministrator
the user must have admin privileges to run the application. --What you mean place everything in a .zip file?
- That you just ship your entire application in a .zip file, no installers? -
user5313398 about 6 years@VisualVincent by having the admin privilege does it get the right to write into the folder automatically or must I do some extra settings on it too?
-
Visual Vincent about 6 years@user5313398 : It should give you writing rights as it gives you such to a much larger variety of folders. But nothing is for certain...
-
user5313398 about 6 years@VisualVincent I set the manifest at first I got this error Severity Code Description Project File Line Suppression State Error ClickOnce does not support the request execution level 'requireAdministrator'. Then under the security tab I disable but it still comes back? So how to resolve on this?
-
Visual Vincent about 6 years@user5313398 : I'm not familiar with ClickOnce, so you'd have to Google that.