9

Properties.Settings を使用して、ASP.net プロジェクトのアプリケーション設定を保存することにしました。ただし、データを変更しようとすると、エラーが発生しますThe property 'Properties.Settings.Test' has no setter。これは生成されるため、以前のすべての C# プロジェクトでこの問題が発生していないため、これを変更するために何をすべきかわかりません。

4

3 に答える 3

19

私の推測では、Applicationスコープではなくスコープでプロパティを定義したと思いますUser。アプリケーション レベルのプロパティは読み取り専用で、web.configファイル内でのみ編集できます。

Settingsこのクラスを ASP.NET プロジェクトではまったく使用しません。ファイルに書き込むとweb.config、ASP.NET/IIS は AppDomain をリサイクルします。設定を定期的に記述する場合は、他の設定ストア (独自の XML ファイルなど) を使用する必要があります。

于 2013-10-03T06:33:14.250 に答える
2

Eli Arbel が既に述べたように、web.config に記述された値をアプリケーション コードから変更することはできません。これは手動でのみ行うことができますが、アプリケーションが再起動するため、これは望ましくありません。

以下は、値を格納し、読み取りと変更を容易にするために使用できる単純なクラスです。XML またはデータベースから読み取る場合、および変更された値を永続的に保存するかどうかに応じて、ニーズに合わせてコードを更新するだけです。

public class Config
{
    public int SomeSetting
    {
        get
        {
            if (HttpContext.Current.Application["SomeSetting"] == null)
            {
                //this is where you set the default value 
                HttpContext.Current.Application["SomeSetting"] = 4; 
            }

            return Convert.ToInt32(HttpContext.Current.Application["SomeSetting"]);
        }
        set
        {
            //If needed add code that stores this value permanently in XML file or database or some other place
            HttpContext.Current.Application["SomeSetting"] = value;
        }
    }

    public DateTime SomeOtherSetting
    {
        get
        {
            if (HttpContext.Current.Application["SomeOtherSetting"] == null)
            {
                //this is where you set the default value 
                HttpContext.Current.Application["SomeOtherSetting"] = DateTime.Now;
            }

            return Convert.ToDateTime(HttpContext.Current.Application["SomeOtherSetting"]);
        }
        set
        {
            //If needed add code that stores this value permanently in XML file or database or some other place
            HttpContext.Current.Application["SomeOtherSetting"] = value;
        }
    }
}
于 2013-10-03T11:24:10.060 に答える
-2

ここ: http://msdn.microsoft.com/en-us/library/bb397755.aspx

あなたの問題の解決策です。

于 2013-10-03T05:48:08.093 に答える