0

I have developed a Windows Form Program in C# with Visual Stuido 2013. When it is installed, I would like the exe.config file to be in the AppData path. Becuase sometimes the program will have to read and write that file and I don't want the program has to be executed with administrator rights.

When I create the installer with a setup project, I add to the Application Folder the "Primary output from the program", but I can not specify that the exe.config may place into the AppData path.

Is there some way to do that in order to avoid the administrator rights?

Thank you.

Santi
  • 33
  • 5
  • 1
    Consider another model where you leave the config file alone and manage a separate xml file with settings in it form the AppData path - just a suggestion. You can read a config file from a non-standard location using `ConfigurationManager.OpenExeConfiguration` (https://learn.microsoft.com/en-us/dotnet/api/system.configuration.configurationmanager.openexeconfiguration?view=netframework-4.7.2#System_Configuration_ConfigurationManager_OpenExeConfiguration_System_String_). If you want to place it somewhere specific, you'll need to do that as part of your install routine – Flydog57 Feb 15 '19 at 15:11
  • Ok, thank you! It seems a good solution. However I have I doubt; I have a layer with EntityFramework that takes the connectionString automaticaly from the exe.config. How can I do so that the EntityFramwork go to the "new cofing" for the stringconnection? – Santi Feb 15 '19 at 15:20
  • I'm not an EF expert, but I'm pretty sure you can override the automatic behavior. Write a little `GetConnectionString` routine. It first uses the normal mechanism. Then, if there's an override settings file in AppData, and it has a connection string, it just loads it from there and returns that one instead (or the other way around, whatever makes sense from you). A connection string is just a string. Now, if you have the connection string section encrypted, it gets more, uh, interesting. Is it encrypted? You may want to add "EF" and "Connection String" to your question – Flydog57 Feb 15 '19 at 15:34
  • Yeah, in fact I am encrypting all the config file, that is why I write it at the beggining. The code is: private void EncryptarAppConfig() { Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); config.ConnectionStrings.SectionInformation.ProtectSection(null); config.Save(ConfigurationSaveMode.Full, true); } – Santi Feb 15 '19 at 15:41
  • I will add also EF and Connection String to the question, thank you for the suggestion, Flydog57 – Santi Feb 15 '19 at 15:42
  • I think there may be a way to add another config file to the chain of config files that the Configuration Manager uses in establishing the current configuration. However, I don't know what it is (nor do I know that, if it does exist, whether it's a web.config-only thing, or if it's available for appname.exe.config files). It's just something in the back of my head from 10 years ago – Flydog57 Feb 15 '19 at 15:49
  • Take a look at: https://stackoverflow.com/questions/4454016/multiple-app-config-files-in-net-class-library-project – Flydog57 Feb 15 '19 at 16:38

2 Answers2

3

using System.Configuration; using System.IO;

    Configuration config = null;

    public Program()
    {       
        string app_data = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);

        StreamWriter sw = new StreamWriter(app_data + "\\MyApp.exe.config", false);
        sw.WriteLine("<?xml version=\"1.0\" encoding=\"utf-8\" ?>");
        sw.WriteLine("<configuration>");
        sw.WriteLine("</configuration>");
        sw.Close();

        ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
        fileMap.ExeConfigFilename = app_data + "\\MyApp.exe.config";
        config = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);

        config.AppSettings.Settings.Add("Key", "Value");
        config.Save();

        Console.ReadLine();
    }
  • Thank you Gabriel, it would be the solution for the question to place the appconfig in AppData. I have the matter that in the installation in WF the appconfig can not be encrypted, you have to do it in the first execution of the code. So, in that way the appconfig in the aplication path would be always unencrypted. – Santi Feb 19 '19 at 09:56
1

Why not just install the application in AppData?

        string[] files = Directory.GetFiles(".\\");

        string app_data = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + "\\Apps\\MyApp";
        Directory.CreateDirectory(app_data);

        foreach (var file in files)
        {
            File.Copy(file, app_data + file.Substring(file.LastIndexOf("\\")) );
        }