11

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?

Visual Vincent
  • 18,045
  • 5
  • 28
  • 75
user5313398
  • 713
  • 3
  • 9
  • 28
  • What is your target environment and how much control do you have over it? Is it for an enterprise organization or personal use? – Justin Loveless Sep 16 '16 at 16:05
  • My target is more of personal use but it can be on enterprise too. – user5313398 Sep 16 '16 at 16:08
  • 1
    Stop trying to copy files to your application folder, that folder can be replaced if the user repairs or reinstalls your app. Use `ApplicationData` instead. – Dour High Arch Sep 16 '16 at 16:10
  • I cant use the application data to be installed. So must I change some settings for me it to be installed in the application data ? – user5313398 Sep 16 '16 at 16:26

5 Answers5

10

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?

Community
  • 1
  • 1
Visual Vincent
  • 18,045
  • 5
  • 28
  • 75
  • @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 so I just change this to – user5313398 Sep 16 '16 at 15:42
  • @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 Sep 16 '16 at 15:45
  • @user5313398 : Yes, you just change the already existing `requestedExecutionLevel` tag. – Visual Vincent Sep 16 '16 at 15:47
  • So what is your current best suggestion to uninstall it first? – user5313398 Sep 16 '16 at 15:47
  • @user5313398 : According to [**this MSDN article about ClickOnce**](https://msdn.microsoft.com/en-us/library/t71a733d.aspx), 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 Sep 16 '16 at 15:51
  • @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? – Visual Vincent Sep 16 '16 at 15:52
  • 1
    @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 Sep 16 '16 at 15:53
  • Also, 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. – Justin Loveless Sep 16 '16 at 15:55
  • @JustinLoveless so what is your suggestion for my case? I need to upload the file into same folder where its installed? – user5313398 Sep 16 '16 at 15:55
  • @JustinLoveless I am surprised why it worked in other pc well. So what is your best suggestion – user5313398 Sep 16 '16 at 15:58
  • @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. – Visual Vincent Sep 16 '16 at 15:58
  • @user5313398 : See if this article might help you: [**How to: Include a Data File in a ClickOnce Application**](https://msdn.microsoft.com/en-us/library/6fehc36e.aspx). – Visual Vincent Sep 16 '16 at 16:00
  • @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. – Justin Loveless Sep 16 '16 at 16:03
  • @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 Sep 16 '16 at 16:05
  • @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. – user5313398 Sep 16 '16 at 16:06
  • @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 Sep 16 '16 at 16:10
  • @JustinLoveless so will this help in any case ? – user5313398 Sep 16 '16 at 16:22
  • @user5313398 : It will help if the user has the authority to run the app with administrator privileges. – Visual Vincent Sep 16 '16 at 20:15
  • @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 the `requireAdministrator` level he must switch to a normal installer (which requires admin privileges anyway) or just place everything in a .zip file. – Visual Vincent Sep 16 '16 at 20:17
  • @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 Sep 16 '16 at 20:20
  • Either 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). – Visual Vincent Sep 16 '16 at 20:21
  • @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 Sep 17 '16 at 03:41
  • What you mean place everything in a .zip file? – user5313398 Sep 17 '16 at 03:42
  • @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**](https://support.microsoft.com/en-us/help/14200/windows-compress-uncompress-zip-files), no installers? – Visual Vincent Sep 17 '16 at 07:03
  • @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? – user5313398 Sep 18 '16 at 15:48
  • @user5313398 : It _**should**_ give you writing rights as it gives you such to a much larger variety of folders. But nothing is for certain... – Visual Vincent Sep 18 '16 at 15:50
  • @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? – user5313398 Sep 20 '16 at 02:38
  • @user5313398 : I'm not familiar with ClickOnce, so you'd have to [Google that](https://www.google.com/search?q=ClickOnce+with+requireAdministrator). – Visual Vincent Sep 20 '16 at 06:57
3

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.

Nimantha
  • 6,405
  • 6
  • 28
  • 69
Ziaullah Khan
  • 2,020
  • 17
  • 20
1

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.

Justin Loveless
  • 524
  • 1
  • 10
  • 25
1

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);
Nimantha
  • 6,405
  • 6
  • 28
  • 69
jb.
  • 1,848
  • 9
  • 27
  • 43
0

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.

RT.
  • 435
  • 2
  • 9
  • 25