使用同一应用程序在执行时编辑app.config

发布于 2024-10-12 06:33:48 字数 753 浏览 3 评论 0原文

我有一个 Windows 窗体应用程序 VS 2008 - C#,它使用 app.config。

在执行时,在我的应用程序的菜单选项中,我想要编辑 app.config 的值,保存它并重新启动应用程序。

任何示例源代码,任何好的模式和实践?

编辑:

在 MSDN 论坛中,Jean Paul VA:

  1. 创建一个测试 Windows 窗体应用程序并向其中添加一个 app.config。

  2. 添加对 System.confguration 的引用

  3. 在 appSettings 中添加一个名为“font”的键,值为“Verdana”

  4. 在表单上放置一个按钮,然后单击它添加修改代码。

     System.Configuration.Configuration 配置 = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
    
        配置.AppSettings.Settings.Remove(“字体”);
        配置.AppSettings.Settings.Add("字体", "Calibri");
    
        配置.保存(ConfigurationSaveMode.Modified);
    
        ConfigurationManager.RefreshSection(“appSettings”);
    

你对此有何看法?

I have an Windows Forms application VS 2008 - C#, that uses app.config.

In execution time, in Menu option of my application, I want editing values of app.config, save it and restart application.

any sample source code, any good patterns and practices ??

edit:

in MSDN Forums, Jean Paul VA:

  1. Create an test windows forms application and add an app.config into it.

  2. Add reference to System.confguration

  3. Add a key named "font" in appSettings with value "Verdana"

  4. Place a button on form and on click of it add the modification code.

        System.Configuration.Configuration configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
    
        configuration.AppSettings.Settings.Remove("font");
        configuration.AppSettings.Settings.Add("font", "Calibri");
    
        configuration.Save(ConfigurationSaveMode.Modified);
    
        ConfigurationManager.RefreshSection("appSettings");
    

what you think about it ?

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

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

发布评论

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

评论(4

吾性傲以野 2024-10-19 06:33:48

我认为您实际上无法在运行时写入配置文件 - 它很可能是只读的,但是,可能有一种方法可以通过重写文件(根据需要进行适当的更改)并基本上替换现有的,然后,进一步重新启动应用程序以加载新值(我非常怀疑这是可取的,并且我个人不会尝试检测这个胡言乱语)。

您也可以考虑将应用程序设置存储在设置文件中,这样可以轻松操作。

要使用设置,首先假设您有一个 Settings.settings 文件(如果没有,则创建一个:添加项目 -> 设置文件),然后我们配置一个名为 MyUnicornsName 的设置;为了进行更改并保留它,您可以简单地执行以下操作:

Settings.Default.MyUnicornsName = "Lucifers Spawn";
Settings.Default.Save();

同样,要读取设置:

ATextDisplayControl.Text = Settings.Default.MyUnicornsName

如果您不知道,当您在 IDE 中打开/双击设置文件时,Visual Studio 将打开设置编辑器;使用此界面,您可以添加和编辑初始设置及其值,并且 string 不是唯一受支持的值,所有原语都可以使用,而且据我所知,也可以使用任何可序列化的值。

I don't think you can actually write to the configuration file at runtime - it may well be read-only, however, there may be a way around this by re-writing the file (with proper alterations as required) and essentially replacing the existing one, then, further, restarting the application to load the new values (I highly doubt this is desirable and personally would not try to instrument this malarkey).

You may also just consider storing application settings in the settings file which are easily manipulated in this way.

To use settings, first let's assume you have a Settings.settings file (if not then create one: Add Item->Settings File), then we have a setting configured named MyUnicornsName; In order to make a change and persist it you can simply do as follows:

Settings.Default.MyUnicornsName = "Lucifers Spawn";
Settings.Default.Save();

Similarly, to read a setting:

ATextDisplayControl.Text = Settings.Default.MyUnicornsName

In case you don't know, Visual Studio will open the settings editor when you open/double click the settings file in the IDE; using this interface you can add and edit your initial settings and their values, and string is not the only supported value, all primitives can be used, and, as far as I know, any serializable value too.

吃兔兔 2024-10-19 06:33:48

有什么原因不能使用通常的自动生成的 Properties.Settings 将更改的数据存储在设置文件中吗?一件很棒的事情是您知道自己正在更改什么,因此您甚至不必重新启动应用程序!

在 C# 中使用设置

的运行时访问设置就像这样简单:(

this.BackColor = Properties.Settings.Default.myColor;

没有好的模式可以简单地修改 app.config 本身,因为它被设计为在该上下文中为只读,具有专家用户设置。)

Is there any reason you can't use the usual auto-gen'd Properties.Settings to store the changing data in a settings file instead? One great thing is that you know what you're changing so you don't even have to restart the application!

Using Settings in C#

Runtime access of settings is as easy as:

this.BackColor = Properties.Settings.Default.myColor;

(There is no good pattern for modifing app.config itself simply b/c it's designed to be readonly in that context, with expert-user settings.)

打小就很酷 2024-10-19 06:33:48

使用“项目”->“属性”->“设置”来执行此类操作。

Use the Project->Properties->Settings for these kinds of things.

帅的被狗咬 2024-10-19 06:33:48

实际上 App.config 中的属性是只读的,所以你不能这样做。

但是有一个技巧..............................

在 Settings.cs 文件中创建一个公共的函数或方法,以便它可以可使用 Properties.settings

并编写以下代码。

    public void ChangeProperty(string propertyname, string value)
    {
        this[propertyname] = value;
    }

记住将属性名称的确切字符串传递给方法。或者更好地为设置创建一个 Writeonly 属性。

更新

这里是设置为属性的代码,我以连接字符串为例,我打赌它可以是任何东西。请记住存储的属性是 Object 类型,因此您可以创建特定于该类型的属性。

    public string MyCustomConnectionstring
    {
        set
        {
            //replace the string with your connection string otr what ever setting you want to change
            this["myConnectionString"] = value;
        }
    }

现在您可以轻松地使用此属性在运行时更改 ConnectionString...

well actually the Properties in the App.config are ReadOnly so u cant do it.

But there's a trick............................

In the Settings.cs file create a Function or method that is public so that it can be available with Properties.settings

and write the following code..

    public void ChangeProperty(string propertyname, string value)
    {
        this[propertyname] = value;
    }

remember to pass the exact string of the property name to the method. or better create a Writeonly Property for the setting.

update

here is a code for setting as a property, i am taking a connection string as an example, bet it can be anything. remember the property stored is of Object type so you can create property specific to that..

    public string MyCustomConnectionstring
    {
        set
        {
            //replace the string with your connection string otr what ever setting you want to change
            this["myConnectionString"] = value;
        }
    }

Now you can easily use this Property to change the ConnectionString at run time...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文