-1

I can copy files to the C:\Program Files (x86)\... ...but only with the administrator account.

This file is a configuration file like App.Config, but with a different name, and I want to put it into the application path's sub folder.

The scenario is like following.

  1. Put the initial configuration file in the \Cfg\org.cfg

  2. When start application program,myApp.exe, check if configuration file exists in the application path.

  3. If it doesn't exist, copy org.cfg to the myApp.exe.config and load it.

I tried DirectoryInfo.SetAccessControl() but following error occur as well.

"Attempted to perform an unauthorized operation"

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
Don
  • 23
  • 1
  • 4
  • The program files folder has restricted access permissions for a reason. Think about a way that will allow you to place your configuration file(s) in the user folder... –  Mar 29 '19 at 18:04
  • Does the app run as an administrator? Right click the exe and in the Properties => Compatibility => Privilege Level, check the "Run this program as an administrator". FWIW, I wouldn't use this location to store app settings that change. `%AppData%` is more for such uses – Vikhram Mar 29 '19 at 18:06
  • 4
    Program Files is the **WRONG PLACE** for this file. It should go in %AppData% or All Users App Data. – Joel Coehoorn Mar 29 '19 at 18:09
  • 1
    This copying should be done during installation by a setup program that has administrator permissions. If the config should be written every time use appdata and if it contains user specific information it should use userdata folders. – Emond Mar 29 '19 at 18:09

1 Answers1

2

Program Files (x86) required administrative permission to write to. So does changing the permissions, which is why SetAccessControl fails. There's really no way around that. If you want to have your configuration file writable by the application, then you might consider changing the permission of the configuration file during installation of the program when you do have administrative access.

I would instead however recommend putting your configuration file somewhere in the user's profile, which is writable by default for all users, like in %LOCALAPPDATA%.

You can load configuration files from arbitrary locations as well, it doesn't have to be in the .config file next to the executable. For an example of that, look at this question.

vcsjones
  • 138,677
  • 31
  • 291
  • 286