c# - 如何将应用程序设置共享给所有可以在运行时更改的用户

标签 c# vb.net visual-studio-2010 .net-4.0 application-settings

我需要一个应用程序的一些设置,该设置将在计算机的所有用户之间共享,但也可以在运行时更改。那个接缝简单,但是根据Application Settings MSDN article , 非此即彼。

There are two types of application settings, based on scope:

  • Application-scoped settings can be used for information such as a URL for a Web service or a database connection string. These values are associated with the application. Therefore, users cannot change them at run time.

  • User-scoped settings can be used for information such as persisting the last position of a form or a font preference. Users can change these values at run time.

我可以编写代码来编辑 app.config XML 文件,但由于它位于程序目录中,因此它在 Windows 7 下受到保护。因此,如果不提升程序或使用 NTFS 权限,这是不可能的。

所以我需要将配置文件写入一个普通文件夹,如 System.Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData)

但这是一个相当普遍的要求!

所以,我想知道是否有一种简单的方法可以实现这一目标而无需重新发明轮子,或者我是否必须编写自己的设置管理器

最佳答案

阅读此处的答案并使用 ConfigurationManager.Open 方法后,我得到了以下结果:

string path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "MyApp", "MyApp.config");
Configuration MyAppConfig = ConfigurationManager.OpenMappedExeConfiguration(new ExeConfigurationFileMap { ExeConfigFilename = path }, ConfigurationUserLevel.None);

好的部分是文件不必存在,如果您对其调用 Save(),它将为您创建文件夹和文件。 AppSettings["key"] 没有用,所以我不得不使用

MyAppConfig.AppSettings.Settings["key"].Value

读取和写入现有值

MyAppConfig.AppSettings.Settings.Add("key", value)

添加新值。

关于c# - 如何将应用程序设置共享给所有可以在运行时更改的用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16306605/

相关文章:

c# - 如何在 UserControl 中创建部分客户区?

c# - ObjectListView 转换异常(用于 HitTest )

visual-studio-2010 - 从序列图创建代码

performance - MSBUILD (VS2010) 在某些机器上非常慢

c# - 如何隐藏控制台应用程序的黑屏?

c# - 延迟函数中的 System.IO.FileNotFoundException

c# - 在小于N的素数中,求2基系中对应的数中含有最多1-s的数

c# - 在不知道类型的情况下返回通用对象?

vb.net - 如何使用 vb.net 获取 X、Y 处像素的颜色

c# - ORA-12541 TNS :no listener on Oracle. DataAccess 连接打开