2013-10-03 21 views
9

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

答えて

17

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

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

-1

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

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

+0

は、私の問題は、 'Settings.Default.LastPayNumber + = 1;' '彼はASPでの' – ChaoticLoki

+0

「プロパティ 『Properties.Settings.LastPayNumnerは』はセッターを持っていない」と言います.NET Webサイト。あなたのリンクは適用されません。 –

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; 
     } 
    } 
} 
関連する問題