我决定使用Properties.Settings来存储ASP.net项目的一些应用程序设置。但是,在试图修改数据时,我得到了一个错误The property 'Properties.Settings.Test' has no setter,因为这是生成的,所以我不知道应该做什么来更改它,因为我以前的所有C#项目都没有这个问题。
发布于 2013-10-03 06:33:14
我的猜测是,您用Application作用域而不是User作用域定义了属性。应用程序级别的属性是只读的,只能在web.config文件中编辑.
我根本不会在Settings项目中使用ASP.NET类。当您写入web.config文件时,ASP.NET/IIS将回收AppDomain。如果您定期编写设置,则应该使用其他设置存储(例如,您自己的XML文件)。
发布于 2013-10-03 11:24:10
正如Eli已经说过的那样,您不能从应用程序代码中修改用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-03 05:48:08
这里:http://msdn.microsoft.com/en-us/library/bb397755.aspx
是解决你问题的方法。
https://stackoverflow.com/questions/19151253
复制相似问题