【发布时间】:2013-10-09 16:23:18
【问题描述】:
我决定使用 Properties.Settings 来为我的 ASP.net 项目存储一些应用程序设置。但是,在尝试修改数据时,我收到错误 The property 'Properties.Settings.Test' has no setter,因为这是生成的,我不知道应该怎么做才能更改它,因为我以前的所有 C# 项目都没有这个问题。
【问题讨论】:
标签: c# asp.net settings.settings
我决定使用 Properties.Settings 来为我的 ASP.net 项目存储一些应用程序设置。但是,在尝试修改数据时,我收到错误 The property 'Properties.Settings.Test' has no setter,因为这是生成的,我不知道应该怎么做才能更改它,因为我以前的所有 C# 项目都没有这个问题。
【问题讨论】:
标签: c# asp.net settings.settings
我的猜测是您使用Application 范围定义了该属性,而不是User 范围。应用级属性是只读的,只能在web.config 文件中编辑。
我根本不会在 ASP.NET 项目中使用 Settings 类。当您写入web.config 文件时,ASP.NET/IIS 会回收 AppDomain。如果您定期编写设置,您应该使用其他一些设置存储(例如您自己的 XML 文件)。
【讨论】:
正如 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;
}
}
}
【讨论】:
这里: http://msdn.microsoft.com/en-us/library/bb397755.aspx
是您问题的解决方案。
【讨论】:
Settings.Default.LastPayNumber += 1; 说"The Property 'Properties.Settings.LastPayNumner' has no setter"