読書デフォルトのアプリケーションアカウント設定アプリケーション

StackOverflow https://stackoverflow.com/questions/49269

質問

い数のアプリケーションアカウント設定(ユーザ)自分のカスタムグリッド。ほとんどの色を設定します。している場合、ユーザーカスタマイズするこれらの色合いたい追加ボタンに戻デフォルトの色を設定します。どのようにはしていなかったのデフォルト設定は?

例えば:

  1. いユーザ設定名 CellBackgroundColorProperties.Settings.
  2. 設計時間の値を設定します CellBackgroundColorColor.White 使用可能です。
  3. ユーザーセット CellBackgroundColorColor.Black 私のプログラム。
  4. 私は保存の設定 Properties.Settings.Default.Save().
  5. ユーザがクリックする Restore Default Colors ボタンを押します。

現在、 Properties.Settings.Default.CellBackgroundColor を返します Color.Black.どうか Color.White?

役に立ちましたか?

解決

@ozgur,

Settings.Default.Properties["property"].DefaultValue // initial value from config file

例:

string foo = Settings.Default.Foo; // Foo = "Foo" by default
Settings.Default.Foo = "Boo";
Settings.Default.Save();
string modifiedValue = Settings.Default.Foo; // modifiedValue = "Boo"
string originalValue = Settings.Default.Properties["Foo"].DefaultValue as string; // originalValue = "Foo"

他のヒント

読みは"Windows2.0形プログラミング"のいずこれらの2つの有用な方法で助けになることがこのコンテクスト:

ApplicationSettingsBase.Reload

ApplicationSettingsBase.リセット

らにMSDN:

リロードトリセットする 前者は負荷の最後のセット 保存アプリケーションの設定値 であるのに対し、後者は負荷の保存 デフォルト値です。

を利用することはできない。

Properties.Settings.Default.Reset()
Properties.Settings.Default.Reload()

Imいく必要がある必要がありますneater、その他希望の誰かつ有用であること。

public static class SettingsPropertyCollectionExtensions
{
    public static T GetDefault<T>(this SettingsPropertyCollection me, string property)
    {
        string val_string = (string)Settings.Default.Properties[property].DefaultValue;

        return (T)Convert.ChangeType(val_string, typeof(T));
    }
}

利用状況、

var setting = Settings.Default.Properties.GetDefault<double>("MySetting");

Properties.Settings.Default.Reset() ますのすべてリセット設定を元の値です。

もう丸したことでこの問題を2セットを設定します。使っているものVisual Studioの追加によりデフォルトの現在の設定、すなわち Properties.Settings.Default.私も他の設定ファイルをプロジェクト"プロジェクト->新規追加項目->一般->設定ファイル"を実際のデフォルト値がある Properties.DefaultSettings.Default.

たいていることを確認しているんへの書き込み Properties.DefaultSettings.Default 設定だからです。変化するもののデフォルト値はそれかを設定する場合には、現在の価値にデフォルト値です。

どうか。白ですか?

つことができるの?

  • 保存のコピーの前に設定を専用アプリをインストールします。
  • キャッシュに変更したユーザー設定を保存してください。設定の前に申請が閉じます。

その呼び出し ApplicationSettingsBase.Reset ての効果をリセットの設定でそのデフォルト値が保存している。

行動欲しかったのはリセットしてデフォルト値がないから受け入れているでしょうか(いした場合には、ユーザーのなかったように、デフォルトしようとすると、保存されたかに戻します。

しっかりとした拡張する方法に適した目的:

using System;
using System.Configuration;

namespace YourApplication.Extensions
{
    public static class ExtensionsApplicationSettingsBase
    {
        public static void LoadDefaults(this ApplicationSettingsBase that)
        {
            foreach (SettingsProperty settingsProperty in that.Properties)
            {
                that[settingsProperty.Name] =
                    Convert.ChangeType(settingsProperty.DefaultValue,
                                       settingsProperty.PropertyType);
            }
        }
    }
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top