3

I have a solution (S) in Visual Studio containing 3 projects, I'll call them CL, WPF, and CFG;

  • S.CL is a project for a console application.
  • S.WPF is a project for a WPF application.
  • S.CFG is a project that contains the config file.

I have created a settings file with user scope and public visibility. Both of these projects use the same settings.

S.CL only needs to read these settings. S.WPF is a form to edit these settings, so it requires read and write. It contains a save button connected to an event handler.

S.CFG app.config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
            <section name="S.CFG.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
        </sectionGroup>
    </configSections>
    <userSettings>
        <S.CFG.Properties.Settings>
            <setting name="ExampleSetting" serializeAs="String">
                <value>ExampleValue</value>
            </setting>
        </S.CFG.Properties.Settings>
    </userSettings>
</configuration>

S.CL Execute.cs:

namespace S.CL
{
    public class Execute
    {
        public static void Main(String[] args)
        {
            Console.WriteLine(S.CFG.Properties.Settings.Default.ExampleSetting);
        }
    }
}

S.WPF MainWindow.xaml.cs:

namespace S.WPF
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void button_Click(object sender, RoutedEventArgs e)
        {
            S.CFG.Properties.Settings.Default.ExampleSetting = "New value";
            Console.WriteLine(S.CFG.Properties.Settings.Default.ExampleSetting);
        }
    }
}

So, steps to reproduce:

Run S.CL out> ExampleValue
Run S.WPF out> New value
Run S.CL out> ExampleValue

On the last run of S.CL, the output should have been "New value".

I can confirm that S.WPF also gets the newest value every time it runs. In my full application, the form displays the newest value in a text box before hitting save.

So far I've managed to find out the S.WPF creates a folder with this new app.config in the user's AppData folder. S.CL uses it's built in copy of the default config, whereas S.WPF uses it's built in copy of the default config until it writes, where it then creates a local copy that only that executable accesses.

I just want to be able to persist these settings between these 2 executables. I have thought about other ways of doing this, another would be to keep an XML file of settings in the root directory of both executables and just parse this file but I would rather using Visual Studio's app.config/Settings.Settings functionality to do this (if possible).

Thanks in advance!

1 Answers1

5

instead of adding the app.config files to each project, you could have one app.config file in your solution and link this one file to both of the projects, as described here: Share code with Add as Link.

Edit:

If you want to be able to load an external app.config, you can check follow the instructions of this post,

Community
  • 1
  • 1
Rico Herlt
  • 136
  • 8
  • 1
    I haven't added an app.config to each project. I have only one, it is in the CFG project. All of the other projects are accessing this CFG settings file, but any time a write is made, a local copy of the settings is made in AppData. Also, I'm looking to use any built in features of C# settings (if they exist) rather than just parsing the config file myself. – Jack Andrew McKay Jun 07 '16 at 13:53
  • 1
    Okay so if i got you, then you have after the build three application which have each an own _app.config_ file. Since each application is loading their own _app.config_ per default at application startup (in the AppData folder of each application), you could manually load the common _app.config_ file by giving the exact path to the common file [as described here](http://stackoverflow.com/a/506637/6435375) – Rico Herlt Jun 07 '16 at 14:03