2017-07-19 8 views
1

私は、このようなコードを使用して、以前のバージョンからのユーザー設定を保存することができることを理解するようになった:設定のローミングプロパティを変更すると、ユーザー設定の値を保持するにはどうすればよいですか?

 if (Settings.Default.UpgradeRequired) 
     { 
      Settings.Default.Upgrade(); 
      Settings.Default.UpgradeRequired = false; 
      Settings.Default.Save(); 
     } 

しかし、私は設定のローミングプロパティを変更した場合に動作するようには思えません。ローミングからローカルに、またはその逆に設定を変更するときに、設定値を持ち越してリセットしないようにする方法はありますか?

編集:GetPreviousVersion()メソッドを使用してローミング設定をローカル設定にアップグレードする可能性のある方法を検討しましたが、現在の設定がローミングしていない場合は以前のバージョンのローミングバージョンはまったく返されません。

  1. をMySettingという名前の設定を行います。再現する

  2. MySettingのRoamingプロパティをtrueに変更します。
  3. MySettingの有効範囲がUserであることを確認してください。

    Console.WriteLine(Settings.Default.GetPreviousVersion("MySetting")); 
        Settings.Default.MySetting = "Not the default value."; 
        Settings.Default.Save(); 
    
  4. インクリメントアセンブリバージョン:
  5. は、次のコードを実行します。

  6. 新しい値が出力されることに注意して、コードを再度実行します。
  7. MySettingのローミングプロパティをfalseに変更します。
  8. 再度アセンブリバージョンをインクリメントします。
  9. コードを再度実行し、既定値が出力されていることに注意してください。

答えて

1

あなたは性質が= = falseをローミングに真のローミングから変更されているかを知る場合は、手動で、その後に辞書から属性を削除し、前の値を取得するためにGetPreviousVersionを使用し、SettingsProperty.Attributes辞書にSettingsManageabilityAttributeを追加することができますクリーンアップ:

Console.WriteLine("Current: {0}", Settings.Default.MySetting); 
// we don't see the previous value here... 
Console.WriteLine("Previous: {0}", Settings.Default.GetPreviousVersion("MySetting")); 
// ...so we manually add the SettingsManageabilityAttribute to it... 
var setting = Settings.Default.Properties["MySetting"]; 
setting.Attributes.Add(typeof(SettingsManageabilityAttribute), new SettingsManageabilityAttribute(SettingsManageability.Roaming)); 
// ...retrieve the previous value... 
Console.WriteLine("Previous: {0}", Settings.Default.GetPreviousVersion("MySetting")); 
// ...and then clean up after ourselves by removing the attribute. 
setting.Attributes.Remove(typeof(SettingsManageabilityAttribute)); 
// ...now we don't see the previous value anymore. 
Console.WriteLine("Previous: {0}", Settings.Default.GetPreviousVersion("MySetting")); 
関連する問題