写入配置文件问题

发布于 2024-09-18 06:37:53 字数 1139 浏览 0 评论 0原文


这段代码工作正常,我的配置文件更改正确。

    //Local Variable Declaration
System.Configuration.Configuration oConfig =
    System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(
    Request.ApplicationPath);

if (oConfig .AppSettings.Settings["CompanyName"] == null)
{
    oConfig AppSettings.Settings.Add("CompanyName", "MyCompanyName");
    oConfig .Save();
}

但是当我想为此目的使用属性时,配置文件中没有发生任何事情。

// 属性声明

private System.Configuration.Configuration _oRootConfig;

public System.Configuration.Configuration oRootConfig
{
   get
   {
       return
           System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(
           Request.ApplicationPath);           

   }
   set { _oRootConfig = value; }
}

if (oRootConfig.AppSettings.Settings["CompanyName"] == null)
{
   oRootConfig.AppSettings.Settings.Add("CompanyName", "MyCompanyName");
   oRootConfig.Save(System.Configuration.ConfigurationSaveMode.Modified, true);
}

现在我有两个问题:
1-为什么这段代码不起作用,还有 没有错误。
2-如果我想以面向对象的方式编程 方式,我能做些什么来修复这个属性 如果问题与财产有关。 谢谢

hi
this code works fine and my config file changes correctly.

    //Local Variable Declaration
System.Configuration.Configuration oConfig =
    System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(
    Request.ApplicationPath);

if (oConfig .AppSettings.Settings["CompanyName"] == null)
{
    oConfig AppSettings.Settings.Add("CompanyName", "MyCompanyName");
    oConfig .Save();
}

but when I want to use a property for this purpose Nothing happend in Config File.

// Property Declaration

private System.Configuration.Configuration _oRootConfig;

public System.Configuration.Configuration oRootConfig
{
   get
   {
       return
           System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(
           Request.ApplicationPath);           

   }
   set { _oRootConfig = value; }
}

if (oRootConfig.AppSettings.Settings["CompanyName"] == null)
{
   oRootConfig.AppSettings.Settings.Add("CompanyName", "MyCompanyName");
   oRootConfig.Save(System.Configuration.ConfigurationSaveMode.Modified, true);
}

now i have two question:
1-why this code doesnot work ,and there
is no error.
2-if i want to programn in object oriented
manner ,what can i do to fix this property
if the problem is related to the property.
thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

沒落の蓅哖 2024-09-25 06:37:56

这行代码:

get 
{ 
return  (System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath)); 
}

set { _oRootConfig = value; }

您没有在 get.txt 文件中设置 _oRootConfig。你需要这段代码:

get
{
     _oRootConfig = (System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath));
     return _oRootConfig;
}

set
{
     _oRootConfig = value;
}

this line of code:

get 
{ 
return  (System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath)); 
}

set { _oRootConfig = value; }

you are not setting _oRootConfig in your get. You need this code:

get
{
     _oRootConfig = (System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath));
     return _oRootConfig;
}

set
{
     _oRootConfig = value;
}
野鹿林 2024-09-25 06:37:55

您要在每次获取时重新打开配置,请改为执行以下操作:

get
{
    if(this._oRootConfig == null)
        this._oRootConfig = (System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath));
    return this._oRootConfig;
}

You're reopening the config on every get, do this instead:

get
{
    if(this._oRootConfig == null)
        this._oRootConfig = (System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath));
    return this._oRootConfig;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文