user5313398
user5313398

Reputation: 753

System.UnauthorizedAccessException: Access to the path "..." is denied

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?

Upvotes: 15

Views: 100723

Answers (6)

John Sagaribay
John Sagaribay

Reputation: 1

This thread came up when Googling this issue. I ended up resolving by granting the trusted installer rights to the folder it was copying the files to. I was unable to run as admin with the .msi.

Upvotes: 0

RT.
RT.

Reputation: 445

In my case, the remote server was returning "." and ".." when I was trying to download (SFTP) files and write to our local/network folder. I'd to explicitly discard the "." and ".." file names.

Upvotes: 0

jb.
jb.

Reputation: 1918

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);

Upvotes: 2

Ziaullah Khan
Ziaullah Khan

Reputation: 2254

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.

Upvotes: 3

Visual Vincent
Visual Vincent

Reputation: 18320

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?

Upvotes: 10

Justin Loveless
Justin Loveless

Reputation: 522

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.

Upvotes: 0

Related Questions