2

Using Microsoft Visual Studio Community 2019.

How can I set a value (a path) in a Configuration File, so it can be updated and then restart the application with the updated value (without having to recompile the application)?

This is a legacy project that I don't really know. The most important requirement is to have the capability to change this path without having to recompile the application. This is what I've tried so far:

Preliminary step:

To check what kind of project it is. Based on this question, I check that the .csproj file contains:

<OutputType>WinExe</OutputType>

So, to my understanding the project is a WinForms Application.

Also, I've tried to seach for the GUID found in the .csproj file:

<ProjectGuid>{B1DD5159-A8E8-4DBE-964F-DB9679F60192}</ProjectGuid>

But the seach for that GUID gives no result, and also is not listed on this two GUID list:

Note: The core of the question is based the asumption that this project is a WinForms Application. Please let me know if this the wrong way of finding the type of this project, or even better pointing out the right way of doing it.


First step:

To edit the Configuration File, going to the Solution Explorer, in which I cannot find any .config file, but if I check the files of this project (via Windows Explorer), I found a app.config file.

I update this file outside of Visual Studio (I'll come back to this later), and add a test property. This modification is done following the structure found in the file, no external source to do this modification.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
            <section name="LF10Demo.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
        </sectionGroup>
    </configSections>
    <userSettings>
        <LF10Demo.Properties.Settings>
            <setting name="AutoStepping" serializeAs="String">
                <value>True</value>
            </setting>
            ...
            <setting name="test" serializeAs="String">
                <value>C:\Temp</value>
            </setting>
        </LF10Demo.Properties.Settings>
    </userSettings>
</configuration>

Then, in the code try to get the value:

var appSettings = System.Configuration.ConfigurationManager.AppSettings;
string value1 = System.Configuration.ConfigurationManager.AppSettings["test"];
string value4 = System.Configuration.ConfigurationSettings.AppSettings.Get("test");

None of which bring the value stored in the app.config file, in fact all of then brings null.

Then, update the app.config file following this post configuration file in a Windows Forms C# Application

<?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <appSettings>
        <add key="test" value="C:\Temp" />
      </appSettings>
    </configuration>

Which didn't work either.

Second step

Going back to the modification of the app.config, discart this modifications and add a different Configuration File (after all, the app.config is not visible in the Solution Explorer). This consist in adding a new configuration file app1.config following this post related with configuration file in a Windows Forms C# Application and also this one Adding and reading from a Config file.

So after adding the new configuration file, all my apptemps with System.Configuration.ConfigurationManager.AppSettings brings null.

Also, when adding the this new configuration file, if I choose app.config the show me a message saying

A file with the name 'App.config' already exits...

So after all, the project is (in some way) finding that file.

Other related information:

In the file Settings.Designer.cs if I add the following:

[global::System.Configuration.UserScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Configuration.DefaultSettingValueAttribute("C:\\Temp")]
public string test
{
    get {
        return ((string)(this["test"]));
    }
    set {
        this["test"] = value;
    }
}

And then using this line:

string value8 = Properties.Settings.Default.test;

Is possible to get the value, but this does not satisfy the requirement that the path can be changed without recompiling the application.

Other related links:

This are other links related with adding a configuration file to Visual Studios projects, I've also tried some of this solutions but without obtaining the expected results:

Alejandro Montilla
  • 2,626
  • 3
  • 31
  • 35

1 Answers1

2

Look in your bin directory and you should see a file named YourProjectName.exe.config with a type of XML configuration. Whatever is in your app.config is copied here when you compile the project. You can change the values in code as follows:

static void UpdateAppSettings(string key, string value)
        {
            var configFile = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            var settings = configFile.AppSettings.Settings;
            settings[key].Value = value;
        }
Edney Holder
  • 1,140
  • 8
  • 22
  • Thank you very much @Edney, just one small comment for other readers, since I've not build the project yet the file is in `~/bin/Debug`, but I guess that after the build process the file will be in `bin`. – Alejandro Montilla Jun 27 '19 at 13:52