首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Properties.Settings没有设置器

Properties.Settings没有设置器
EN

Stack Overflow用户
提问于 2013-10-03 05:13:21
回答 3查看 6.5K关注 0票数 9

我决定使用Properties.Settings来存储ASP.net项目的一些应用程序设置。但是,在试图修改数据时,我得到了一个错误The property 'Properties.Settings.Test' has no setter,因为这是生成的,所以我不知道应该做什么来更改它,因为我以前的所有C#项目都没有这个问题。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-10-03 06:33:14

我的猜测是,您用Application作用域而不是User作用域定义了属性。应用程序级别的属性是只读的,只能在web.config文件中编辑.

我根本不会在Settings项目中使用ASP.NET类。当您写入web.config文件时,ASP.NET/IIS将回收AppDomain。如果您定期编写设置,则应该使用其他设置存储(例如,您自己的XML文件)。

票数 19
EN

Stack Overflow用户

发布于 2013-10-03 11:24:10

正如Eli已经说过的那样,您不能从应用程序代码中修改用web.config编写的值。您只能手动完成此操作,但是应用程序将重新启动,这是您不想要的。

下面是一个简单的类,您可以使用它来存储值,并使它们易于阅读和修改。如果您正在从XML或数据库中读取并且取决于是否要永久存储修改过的值,只需更新代码以满足您的需要。

代码语言:javascript
复制
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;
        }
    }
}
票数 2
EN

Stack Overflow用户

发布于 2013-10-03 05:48:08

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

是解决你问题的方法。

票数 -2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19151253

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档