简体   繁体   English

在app.config中设置值-没有对程序文件的写访问

[英]Set value in app.config - no write access to Program Files

I would like to change values in my app.config file (or rather MyProgramName.config file) at runtime. 我想在运行时更改app.config文件(或MyProgramName.config文件)中的值。 This works fine using 这可以正常使用

System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
//update values in config.AppSettings...
config.Save(ConfigurationSaveMode.Modified);

However the MyProgramName.config file is always stored in the program folder according to the documentation ( https://msdn.microsoft.com/de-de/library/system.configuration.configurationuserlevel%28v=vs.110%29.aspx ) 然而MyProgramName.config根据文档文件总是存储在程序文件夹( https://msdn.microsoft.com/de-de/library/system.configuration.configurationuserlevel%28v=vs.110%29.aspx

Application configuration files are in the same directory as the application and have the same name, but with a .config extension. 应用程序配置文件与应用程序位于同一目录中,并且具有相同的名称,但扩展名为.config。 For example, the configuration file for C:\\System\\Public.exe is C:\\System\\Public.exe.config. 例如,C:\\ System \\ Public.exe的配置文件是C:\\ System \\ Public.exe.config。

Now lets assume I have some users who can not write to C:\\Program Files(x86)\\... (which is the default setting for non-admin users on win7). 现在,假设我有一些无法写入C:\\Program Files(x86)\\... (这是win7上非管理员用户的默认设置)。 Is there a way to still use the app.config file properly or maybe place it in the %APPDATA% folder? 有没有办法仍然正确使用app.config文件,或者将其放在%APPDATA%文件夹中?

You can use the User settings capability of the application settings api for this. 您可以为此使用应用程序设置api的用户设置功能。 These settings are stored in %userprofile%\\appdata\\local or %userprofile%\\Local Settings\\Application Data (windows version dependent). 这些设置存储在%userprofile%\\appdata\\local%userprofile%\\Local Settings\\Application Data (取决于Windows版本)中。

User settings has some limitations: it is, as it says, user specific not global for all users - but presumably if you are updating values at runtime then that's what you want, otherwise you need something like this . 用户设置有一定的局限性:它是,因为它说,用户特定不是全球所有用户-但据推测,如果你在运行时更新值,那么这就是你想要的东西,否则你需要的东西像这样

You essentially just have to add a .settings file, which creates the applicationSettings and/or userSettings sections in your app.config (see MSDN: How To: Create a New Setting at Design Time ), create your property and set it to User , not Application , and then do this at runtime: 从本质userSettings ,您只需添加一个.settings文件, userSettings在app.config中创建applicationSettings和/或userSettings部分(请参见MSDN:如何:在设计时创建新设置 ),创建属性并将其设置为User ,不是Application ,然后在运行时执行此操作:

Properties.Settings.Default.myColor = Color.AliceBlue;
Properties.Settings.Default.Save();

The .settings property will create a user settings entry in your app.config that looks like this: .settings属性将在您的app.config中创建一个用户设置条目,如下所示:

<setting name="Setting1" serializeAs="String" >
 <value>My Setting Value</value>
</setting>

You can use this to set the default value that a user session will get before any user-specific value has been saved. 您可以使用此设置在保存任何特定于用户的值之前,用户会话将获得的默认值。

Reference: MSDN: Using Application Settings and User Settings 参考: MSDN:使用应用程序设置和用户设置

Data, stored in app.config , are not intended for changing by regular user at run-time. 存储在app.config数据不适用于普通用户在运行时进行更改。 Do not write a code, which re-writes app.config , except the case, when this code is located at some config application for admin. 如果此代码位于admin的某些配置应用程序中,则请勿编写会重写app.config代码。 Use application settings instead. 请改用应用程序设置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM