如何编辑共享的App.Config文件(WinForms) [英] How to edit a shared App.Config file (WinForms)

查看:85
本文介绍了如何编辑共享的App.Config文件(WinForms)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个分为两部分的备份程序; WinForm(配置)和控制台应用程序(完成所有工作的部分).

这两个部分共享一个单独的App.Config文件,其中包含源目录和目标目录.我已经将file ="属性添加到每个部分的App.Config的appSettings标记中,并且可以成功检索键,但是我似乎无法编辑共享的App.Config .

这是我正在使用的代码

Hi, I''m building a two part backup program; WinForm (Configuration) and Console App (part that does all the work).

Both parts share a single App.Config file which contains the Source and Destination directories. I''ve already added the file="" attribute into the appSettings tag of the App.Config''s of each part and can successfully retrieve the keys but I can''t seem to be able to edit the shared App.Config.

Here is the code that I''m using

Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

  config.AppSettings.Settings["Destination"].Value = @DestinationPath.Text;
  config.Save(ConfigurationSaveMode.Modified,true);
  ConfigurationManager.RefreshSection("appSettings");

推荐答案

最可能的解释是,当您保存配置信息时,它将保存该应用程序的所有数据的当前状态-而不只是写入更改的位.如果您考虑一下,那几乎是它唯一的方法:说完所有内容后,它就是一个文本文件,因此,在字符串中添加单个字符意味着从那时起需要整个文件还是改写了.

因此,如果您有两个程序一起访问文件,那么最后一个要写入的程序将覆盖另一个程序的设置.

可以克服此问题,方法是在更改设置之前重新加载设置,然后立即写入新设置,但前提是这两个应用程序都支持完全相同的一组设置,并且它们不会尝试同时读取或写入.讨厌.

如果需要一个以上的程序来访问文件,那么我将使用多用户容忍的文件格式.如果您可以访问SQl​​或MySql数据库,则可能看起来有些矫kill过正,但可以简化很多事情!如果没有,那么我将考虑使用Access,SQLCE或SQLLite-尽管第一个在多用户环境中相当差,而其他两个根本不打算那样使用,但是如果您进行适当的错误检查,则可以使用它们.来检测文件的使用时间.
The most likely explanation is that when you save the configuration info, it saves the current state of all the data for that app - rather than just writing the changed bits. If you think about it, that is pretty much the only way it can do it: it''s a text file when all is said and done, so adding a single character to a string means teh entire file from that point on needs to be re-written anyway.

So if you have two programs access the file together, the last one to write will overwrite the settings from the other.

You may be able to get over this by re-loading the settings before you change them, then immediately writing the new settings, but only if both applications support exactly the same set of settings, and they don''t try to read or write at the same time. Nasty.

If you need to have more that one program accessing the file, then I would use a multi-user tolerant file format instead. If you have access to an SQl or MySql database instead it may seem overkill but it would simplify things a lot! If not, then I would consider Access, SQLCE or SQLLite - although the first is pretty poor at multi-user, and the other two aren''t intended at all for use like that, they can be used if you do proper error checking to detect when the file is in use.


这篇关于如何编辑共享的App.Config文件(WinForms)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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