Reputation: 127
I have a solution (S) in Visual Studio containing 3 projects, I'll call them CL, WPF, and CFG;
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!
Upvotes: 3
Views: 4579
Reputation: 136
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,
Upvotes: 5