none
How to copy file to the C:\Program Files (x86) at runtime RRS feed

  • Question

  • When I copy file using File.Copy to the C:\Program Files (x86) from my program, following error occur.

    Access to the path 'C:\Program Files (x86)\MyAppFolder\MyApp.exe.config' is denied.

    If I run my application program as administrator, no error occur but I want to make it work with any user account.

    How could do this programmatically


    Friday, March 29, 2019 3:12 PM

Answers

  • As others have stated, you cannot do this by default as PF is locked down. If you installed your app to a different folder then you might be able to. Alternatively the general solution is the installer for your app modifies the write privileges on the folder/files that it wants to write to such that all users can modify it. This is an installer feature though.

    Alternatively don't store your data in config. Configs are for application-wide settings. If you need to adjust app-wide settings then that should probably be the job of your installer. If you need per-user settings then use a file that is stored in the user's Documents directory. ApplicationSettings is designed for this or you can build your own.


    Michael Taylor http://www.michaeltaylorp3.net

    • Marked as answer by Jeff0803 Friday, March 29, 2019 5:31 PM
    Friday, March 29, 2019 5:06 PM
    Moderator
  • However I can copy files to the C:\Program Files (x86)\... with administrator account.

    This file is a configuration file like App.Config with different name.

    So 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 <application path>\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.

    And the purpose of this is what?

    • Marked as answer by Jeff0803 Monday, April 1, 2019 2:54 PM
    Friday, March 29, 2019 10:32 PM
  • Standard access permissions for system folders like Program Files, System32, etc. do not grant standard users write access.  Elevated privilege is an essential requirement.  This is necessary to maintain the integrity of the system.

    Essentially, what you are asking is how to allow a standard user to circumvent essential security controls and achieve privilege escalation.

    Or have I misunderstood the question?


    What is the problem that you are trying to solve?
    • Edited by RLWA32 Friday, March 29, 2019 3:42 PM
    • Marked as answer by Jeff0803 Friday, March 29, 2019 5:20 PM
    Friday, March 29, 2019 3:39 PM

All replies

  • Standard access permissions for system folders like Program Files, System32, etc. do not grant standard users write access.  Elevated privilege is an essential requirement.  This is necessary to maintain the integrity of the system.

    Essentially, what you are asking is how to allow a standard user to circumvent essential security controls and achieve privilege escalation.

    Or have I misunderstood the question?


    What is the problem that you are trying to solve?
    • Edited by RLWA32 Friday, March 29, 2019 3:42 PM
    • Marked as answer by Jeff0803 Friday, March 29, 2019 5:20 PM
    Friday, March 29, 2019 3:39 PM
  • C:\program files is a protected folder.
    Friday, March 29, 2019 3:55 PM
  • As others have stated, you cannot do this by default as PF is locked down. If you installed your app to a different folder then you might be able to. Alternatively the general solution is the installer for your app modifies the write privileges on the folder/files that it wants to write to such that all users can modify it. This is an installer feature though.

    Alternatively don't store your data in config. Configs are for application-wide settings. If you need to adjust app-wide settings then that should probably be the job of your installer. If you need per-user settings then use a file that is stored in the user's Documents directory. ApplicationSettings is designed for this or you can build your own.


    Michael Taylor http://www.michaeltaylorp3.net

    • Marked as answer by Jeff0803 Friday, March 29, 2019 5:31 PM
    Friday, March 29, 2019 5:06 PM
    Moderator
  • However I can copy files to the C:\Program Files (x86)\... with administrator account.

    This file is a configuration file like App.Config with different name.

    So 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 <application path>\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.

    • Edited by Jeff0803 Friday, March 29, 2019 5:42 PM
    Friday, March 29, 2019 5:26 PM
  • Standard users ordinarily have write access to the folders contained in C:\ProgramData
    Friday, March 29, 2019 5:43 PM
  • I described in more detail below as a answer to the CoolDadTx's comment.
    Friday, March 29, 2019 5:46 PM
  • However I need to copy to the C:\Program Files (x86)\.. instead of C:\ProgramData\...
    Friday, March 29, 2019 5:59 PM
  • Isn't there any way to copy to the C:\Program Files (x86) folder programmatically?

    I can copy file to that folder with administrator user in Windows but need to copy in my program(C#)

    Friday, March 29, 2019 6:02 PM
  • Any way you look at it, you need to start with elevated privileges.

    Elevation is necessary to change the security descriptor for folders created under C:\Program Files (x86).

    If you don't change the security descriptor then elevation is needed to write to the folders.

    Another option is to use an intermediary, such as a service, that has elevated privilege to copy the file on behalf of the standard user.

    Friday, March 29, 2019 6:15 PM
  • Modify your installer to give Users write privileges to the appropriate subfolder under your app the point of installation. This is the standard workaround for apps. The only other workaround generally used is to tell users not to install to PF but instead to a writable folder but that generally isn't a good idea.

    Michael Taylor http://www.michaeltaylorp3.net

    Friday, March 29, 2019 6:18 PM
    Moderator
  • However I can copy files to the C:\Program Files (x86)\... with administrator account.

    This file is a configuration file like App.Config with different name.

    So 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 <application path>\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.

    And the purpose of this is what?

    • Marked as answer by Jeff0803 Monday, April 1, 2019 2:54 PM
    Friday, March 29, 2019 10:32 PM
  • The purpose of this scenario is to make my application program work even if the configuration file doesn't exist in the application folder.

    Initially my configuration file is created by combining several data but that process can failed, and the configuration file does not exist in the application folder.

    So I should bring the default configuration file named org.cfg from the <Application folder>\Cfg folder.

    Once I copy it to the application folder(C:\Program Files (x86)\...\MyApp.exe.config), my program will work without failure.

    One more reason of this scenario is because my program doesn't read configuration file correctly if I load default configuration from <Application foler>\Cfg directly using ConfigurationManager.OpenMappedExeConfiguration() and save it to application folder when changed using Configuration.Save(). I tried to solve this issue several days but failed to figure out the reason.

    This is the purpose of this scenario.


    • Edited by Jeff0803 Monday, April 1, 2019 2:54 PM
    Monday, April 1, 2019 2:46 PM
  • If your app needs to read/write files shared by all users on the machine then store the file in the public documents directory that all users have permission to. You can get to this path in .NET via Environment.SpecialFolder.CommonDocuments. You can then read/write to this file whenever you need to. The configuration subsystem can read/write from here using OpenMappedExecConfiguration if needed however, as mentioned in other posts, it is generally easier to use a more flexible format like JSON.

    Michael Taylor http://www.michaeltaylorp3.net

    Monday, April 1, 2019 3:02 PM
    Moderator