Reputation: 945
I have problems with saving user settings on Mono (Ubuntu). Here is code sample:
private void Form1_Load(object sender, EventArgs e)
{
string savedText = Properties.Settings.Default.tbText.ToString();
tbInput.Text = savedText;
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
string textToSave = tbInput.Text;
Properties.Settings.Default.tbText = textToSave;
Properties.Settings.Default.tbText = Properties.Settings.Default.tbText;
Properties.Settings.Default.Save();
}
But i receive only empty config like
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<userSettings />
</configuration>
I've already tried something like
Properties.Settings.Default.tbText = Properties.Settings.Default.tbText;
from here. But still get same result. How can I use settings in Mono?
Upvotes: 4
Views: 1824
Reputation: 74710
I had problems with this, and PiKos advice didn't work for me. Mine was just as you found:
I had an AppName.exe.config alongside my AppName.exe, and this had data like:
<configSections>
<sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<section name="AppName.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
</sectionGroup>
</configSections>
<userSettings>
<AppName.Properties.Settings>
<setting name="X" serializeAs="String">
<value>0</value>
</setting>
</AppName.Properties.Settings>
</userSettings>
I had an ~/.local/share/CompanyName/AppName_4309857324572934859834/user.config
that as effectively empty:
<?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" />
</configSections>
<userSettings />
</configuration>
Note that this sectionGroup
has no children but the one in AppName.exe.config
did
The solution for me was to cut the relevant <sectiongroup>
plus its child, and also <userSettings>
out of AppName.exe.config
and paste them into the empty user.config
It seems that mono expects them to be in only one place. If they're in appname.exe.cofnig
you'll get an empty user.config written. If they're in user.config they will be updated there when Save() is called. The down side of this, I think, is that if user.config ever gets damaged, there isn't any default settings anywhere (I tried PiKos' advice to delete them from AppName.exe.config
and also delete user.config then relauncht he app; user.config was created with all the settings names, but all the values were null)
Upvotes: 1
Reputation: 1384
Remove line
Properties.Settings.Default.tbText = Properties.Settings.Default.tbText;
Check you application config file. Probably you have something like this:
<configuration>
<configSections>
<sectionGroup name="userSettings"
type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<section name="ExampleApp.Properties.Settings"
type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
</sectionGroup>
</configSections>
...
<appSettings>
<add key="TestKey1" value="Test1" />
<add key="TestKey2" value="Test2" />
</appSettings>
...
<userSettings>
<ExampleApp.Properties.Settings>
<setting name="textBox1" serializeAs="String">
<value />
</setting>
</ExampleApp.Properties.Settings>
</userSettings>
</configuration>
Remove sectionGroup for userSettings.
<sectionGroup name="userSettings"
type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<section name="ExampleApp.Properties.Settings"
type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
</sectionGroup>
Remove also section userSettings.
<userSettings>
...
</userSettings>
Delete your local user.config. After first run it will be recreated correctly.
It should work
Upvotes: 2