在app.config文件中写入/读取用户定义的设置 [英] Writing/Reading User-Defined settings in app.config file

查看:340
本文介绍了在app.config文件中写入/读取用户定义的设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想读取和写入用户设置的app.config文件。我发现了一个代码片段的工作与confige文件。我终于得到它的编译和运行,但它绝对好像对于App.config文件什么都不做。



这是代码:

 方法MainForm1.Button1.Click(sender:System.Object; e:System.EventArgs); 
var
config:System.Configuration.Configuration;
begin
config:= ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.AppSettings.Settings.Add('PreferenceToRemember','value1');
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection('appSettings');
end;

这是编译没有任何错误,但我不知道是否正在做任何事情。 p>

代码有什么问题吗?我需要能够写/读一个部分和写/读一个键/值。提前感谢。



更新:不使用ConfigurationManager,我只需使用Properties.Settings.Default。



如何在代码中读写和写入Properties.Settings.Default?

解决方案

你看错了文件?
解决方案中的app.config将被复制到bin / Debug或bin / Release文件夹中的YourProgramFile.exe.config。当运行你的程序时,它会更新这个文件,而不是你的解决方案中的app.config文件。



然后你也应该检查你的应用程序文件夹的写入权限。通常(Win Vista,Win 7),执行应用程序的用户在您的应用程序应该驻留的Program Files文件夹中没有写权限,因此更新.config可能会由于缺少写权限而失败。这对于Linux / Unix系统更是如此。



你应该尝试分离你需要写的元素,并在用户特定的文件夹中写一个附加的配置文件。您可以从最初的普通应用程序配置中获取默认值,只需更新用户特定的配置文件,这样您就不会受到文件权限的阻碍,每个用户都可以更新其设置。


I am trying to read and write into app.config file of user-settings. I found a snippet of a code for working with confige file. I finally got it compiling and running, but it absolutely seems to be doing nothing to the App.config file.

Here is the code:

Method MainForm1.Button1.Click(sender: System.Object; e: System.EventArgs);
var
config  : System.Configuration.Configuration;
begin
     config:=ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
     config.AppSettings.Settings.Add('PreferenceToRemember','value1');
     config.Save(ConfigurationSaveMode.Modified);
     ConfigurationManager.RefreshSection('appSettings');
end;

It is compiling without any errors, but I don't know if it is doing anything.

Is there anything wrong with the code? I need to be able to write/read a section and write/read a key/value. Thanks in advance.

UPDATE: Instead of using ConfigurationManager, I simply use Properties.Settings.Default. However, I am having bit of a problem writing into it and reading back from it, although program complies with without any errors and the code seems simple.

How do you read and write to Properties.Settings.Default from within your code?

解决方案

Maybe you're looking at the wrong file? The app.config you have in your solution will be copied to YourProgramFile.exe.config in the bin/Debug or bin/Release folder. When running your program it will update this file, not the app.config file in your solution.

Then perhaps you also should check write permissions on your application folder. Normally (Win Vista, Win 7) the User executing an application does not have write permissions in the Program Files folder where your application should reside, so updating the .config will most probably fail due to the lack of write permissions. This is even more true for Linux/Unix systems.

You should try to separate the elements you need to write and write an additional config file in a user-specific folder. You can take the defaults from the normal application config for that initially and just update the user-specific config file, this way you are not hindered by file permissions and every user can update their settings specifically.

这篇关于在app.config文件中写入/读取用户定义的设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆