如何修改web.config部分运行时?

时间:2022-06-27 16:03:47

I have created one class that directly map to ConfigSection of web. config. My class definition is given below :

我创建了一个直接映射到Web的ConfigSection的类。配置。我的课程定义如下:

public class myConfiguration: ConfigurationSection
{
    public myConfiguration()
    {
        //
        // TODO: Add constructor logic here
        //
    }

    [ConfigurationProperty("fileName", IsRequired = true)]
    public string FileName
    {
        get { return this["fileName"] as string; }
    }


    [ConfigurationProperty("rootNode", IsRequired = true)]
    public string RootNode
    {
        get { return this["rootNode"] as string; }
    }

    [ConfigurationProperty("childNode", IsRequired = true)]
    public string ChildNode
    {
        get { return this["childNode"] as string; }
    }

    [ConfigurationProperty("comparableAttributes", IsRequired = true)]
    public string ComparableAttributes
    {
        get
        { return this["comparableAttributes"] as string; }
    }
}

I have created section in web.config file as below :

我在web.config文件中创建了如下部分:

    <configSections>
    <section name="myConfigDemo" type="myConfiguration"/>
    </configSections>

Then i have used this section as

然后我用这个部分作为

  <myConfigDemo fileName="myXml.xml" rootNode="world" childNode="country" comparableAttributes="id, population">

  </myConfigDemo>

Now the problem is How can I assign fileName = "anotherFile.xml" at runtime ? I have tried

现在的问题是如何在运行时分配fileName =“anotherFile.xml”?我努力了

   [ConfigurationProperty("fileName", IsRequired = true)]
    public string FileName
    {
        get { return this["fileName"] as string; }
        set {
            string str = this["fileName"] as string; 
              str  = value; }
    }

But my Visual Studio make my pc hang wen i use code above ! i Know the property is readonly when u use only get but set makes my pc hang !!! What can i do to change filename runtime ?

但我的Visual Studio使我的电脑挂起我使用上面的代码!我知道属性是readonly当你只使用get但设置让我的电脑挂!我该怎么做才能更改文件名运行时?

1 个解决方案

#1


2  

There are .net classes designed for more accurate accessing of almost everything that can be found in .config files (and not just appSettings or ConnectionStrings elements); documentation here: http://msdn.microsoft.com/en-us/library/x1et32w6.aspx

有.net类设计用于更准确地访问几乎所有可以在.config文件中找到的内容(而不仅仅是appSettings或ConnectionStrings元素);文档:http://msdn.microsoft.com/en-us/library/x1et32w6.aspx

I'm not sure if they provide ways to change values, though (take a look). However, a gotcha: config files are designed to configure the app at startup; in other words, the app reads the file when it starts, and then again if it's changed manually or by a process. With asp.net apps, this means that the application will automatically restart (by default; IIS setting).

我不确定他们是否提供了改变价值观的方法(看看)。但是,一个gotcha:config文件旨在在启动时配置应用程序;换句话说,应用程序在启动时读取文件,然后再次手动或通过进程更改文件。使用asp.net应用程序,这意味着应用程序将自动重启(默认情况下; IIS设置)。

If you really want to reconfigure the app at runtime, you'll force it to restart every time you SAVE the file. So, in that case, write code to make all changes in memory (e.g. by using xml classes), then save all at once.

如果您真的想在运行时重新配置应用程序,那么每次保存文件时都会强制它重新启动。因此,在这种情况下,编写代码以在内存中进行所有更改(例如,通过使用xml类),然后立即保存所有内容。

There is a setting within app-pool to disable auto-restarting on config changes; however, if you do this, the app will NOT restart when you make config changes, and you'll have to write code to restart it for it to pick up those changes.

app-pool中有一个设置可以禁用配置更改时的自动重启;但是,如果这样做,当您进行配置更改时,应用程序将不会重新启动,并且您必须编写代码才能重新启动它以获取这些更改。

This class might be your friend, if you want to automatically serialize your custom config class to xml element: http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlserializer.aspx

如果您想自动将自定义配置类序列化为xml元素,则此类可能是您的朋友:http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlserializer.aspx

I hope that helps.

我希望有所帮助。

#1


2  

There are .net classes designed for more accurate accessing of almost everything that can be found in .config files (and not just appSettings or ConnectionStrings elements); documentation here: http://msdn.microsoft.com/en-us/library/x1et32w6.aspx

有.net类设计用于更准确地访问几乎所有可以在.config文件中找到的内容(而不仅仅是appSettings或ConnectionStrings元素);文档:http://msdn.microsoft.com/en-us/library/x1et32w6.aspx

I'm not sure if they provide ways to change values, though (take a look). However, a gotcha: config files are designed to configure the app at startup; in other words, the app reads the file when it starts, and then again if it's changed manually or by a process. With asp.net apps, this means that the application will automatically restart (by default; IIS setting).

我不确定他们是否提供了改变价值观的方法(看看)。但是,一个gotcha:config文件旨在在启动时配置应用程序;换句话说,应用程序在启动时读取文件,然后再次手动或通过进程更改文件。使用asp.net应用程序,这意味着应用程序将自动重启(默认情况下; IIS设置)。

If you really want to reconfigure the app at runtime, you'll force it to restart every time you SAVE the file. So, in that case, write code to make all changes in memory (e.g. by using xml classes), then save all at once.

如果您真的想在运行时重新配置应用程序,那么每次保存文件时都会强制它重新启动。因此,在这种情况下,编写代码以在内存中进行所有更改(例如,通过使用xml类),然后立即保存所有内容。

There is a setting within app-pool to disable auto-restarting on config changes; however, if you do this, the app will NOT restart when you make config changes, and you'll have to write code to restart it for it to pick up those changes.

app-pool中有一个设置可以禁用配置更改时的自动重启;但是,如果这样做,当您进行配置更改时,应用程序将不会重新启动,并且您必须编写代码才能重新启动它以获取这些更改。

This class might be your friend, if you want to automatically serialize your custom config class to xml element: http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlserializer.aspx

如果您想自动将自定义配置类序列化为xml元素,则此类可能是您的朋友:http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlserializer.aspx

I hope that helps.

我希望有所帮助。