2016-05-18 46 views
1

我在保存Mono(Ubuntu)上的用戶設置時遇到問題。下面是代碼示例:單聲道無法保存用戶設置

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(); 
} 

但我只收到空的配置一樣

<?xml version="1.0" encoding="utf-8"?> 
<configuration> 
    <userSettings /> 
</configuration> 

我已經嘗試過類似

Properties.Settings.Default.tbText = Properties.Settings.Default.tbText; 

東西從here。但仍然得到相同的結果。我如何在Mono中使用設置?

回答

1
  1. 刪除線

    Properties.Settings.Default.tbText = Properties.Settings.Default.tbText; 
    
  2. 檢查你的應用程序配置文件。也許你有這樣的事情:

    <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> 
    

    刪除sectionGroup爲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> 
    

    刪除也部分userSettings

    <userSettings> 
        ... 
    </userSettings> 
    
  3. 刪除您的本地user.config。第一次運行後,它會被正確地重新創建。

    它應該工作