如何将应用程序范围设置变为只读?

时间:2022-09-11 18:01:55

What use are they if they cannot be altered from their default values anyway? Rhetorical question.

如果它们无法从默认值改变,它们有什么用?修辞问题。

First, what's the best way to circumvent the Settings system and write to the application scope settings file directly (security issues in the next paragraph)? XmlReader/XmlWriter?

首先,绕过设置系统并直接写入应用程序范围设置文件的最佳方法是什么(下一段中的安全问题)?的XmlReader / XmlWriter的?

IIRC, if an application tries to write to its Program Files folder, Windows Vista and 7 does some magic to prevent that. I suppose I need to call UAC to elevate and I must put that fancy shield icon on my application's Options window's OK button and so on. Link to a quick how-to?

IIRC,如果应用程序试图写入其Program Files文件夹,Windows Vista和7会做一些魔术来防止这种情况发生。我想我需要调用UAC进行升级,我必须在我的应用程序的“选项”窗口的“确定”按钮上添加该花式盾牌图标,依此类推。链接到快速操作方法?

Alternatively, you may tell me that what I'm trying to do is stupid and I should store my alterable application scope settings somewhere else altogether.

或者,您可以告诉我,我正在尝试做的是愚蠢的,我应该将我可变的应用程序范围设置存储在其他地方。

3 个解决方案

#1


10  

If you change them to "user" settings they can be changed in code, and when you call Save() they will be saved to a user.config file in the current users local settings folder.

如果将它们更改为“用户”设置,则可以在代码中更改它们,当您调用Save()时,它们将保存到当前用户本地设置文件夹中的user.config文件中。

Obviously, this means they can be different for every user. Generally, global application settings that are the same for every user aren't modified in code because a change that one user makes will effect everyone else (hence the app settings are read only).

显然,这意味着每个用户可能会有所不同。通常,对于每个用户都相同的全局应用程序设置不会在代码中进行修改,因为一个用户所做的更改将影响其他所有人(因此应用程序设置是只读的)。

If you don't want them to be user scoped, take a look at the ConfigurationManager class. This will allow you to manually read and write to .config files. Remember though that the c:\program files\ folder is protected and normal users won't have access to it (this will result in UAC prompts or failure in vista/win7). Consider carefully how you will handle it, and remember that any change to an app.config will affect all users.

如果您不希望它们是用户范围的,请查看ConfigurationManager类。这将允许您手动读取和写入.config文件。请记住,c:\ program files \文件夹受到保护,普通用户将无法访问它(这将导致UAC提示或vista / win7失败)。仔细考虑如何处理它,并记住对app.config的任何更改都将影响所有用户。

There is no location in windows that all users are guaranteed to have write access to.

Windows中没有任何位置可以保证所有用户都具有写访问权限。

#2


0  

Look here: Best practice to save application settings in a Windows Forms Application

查看此处:在Windows窗体应用程序中保存应用程序设置的最佳实践

The ApplicationSettings class doesn't support saving settings to the app.config file. That's very much by design, apps that run with a properly secured user account (think Vista UAC) do not have write access to the program's installation folder.

ApplicationSettings类不支持将设置保存到app.config文件。这非常依赖于设计,使用适当安全的用户帐户(想想Vista UAC)运行的应用程序没有对程序安装文件夹的写入权限。

You can fight the system with the ConfigurationManager class. But the trivial workaround is to go into the Settings designer and change the setting's scope to User. If that causes hardships (say, the setting is relevant to every user), you should put your Options feature in a separate program so you can ask for the privilege elevation prompt. Or forego using a setting.

您可以使用ConfigurationManager类对抗系统。但是,简单的解决方法是进入“设置”设计器并将设置的范围更改为“用户”。如果这会导致困难(例如,设置与每个用户相关),则应将“选项”功能放在单独的程序中,以便可以请求权限提升提示。或放弃使用设置。

#3


0  

An old question, but I provide this answer to help anyone attempting to implement Simon P Stevens' answer related to the ConfigurationManager class since I wasn't sure how to do it being a novice with settings.

一个老问题,但我提供这个答案,以帮助任何人尝试实现Simon P Stevens与ConfigurationManager类相关的答案,因为我不知道如何做一个新手设置。

One of the first realizations was that the 2 Settings files in my C# project (made difficult because the typical one under Properties was there, but empty) were combined into the single .config and split between different ConfigurationSections. I thought that was why ConfigurationManager.AppSettings and ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None) both kept returning 0 keys.

最初的一个实现是我的C#项目中的2个设置文件(因为属性中的典型文件很难,但很空)被合并到单个.config中并在不同的ConfigurationSections之间拆分。我认为这就是为什么ConfigurationManager.AppSettings和ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)都保持返回0键的原因。

It took a lot of trial and error to realize that most of the ConfigurationManager references deal with the default appSettings and that was different than applicationSettings which is what Settings uses.

经过大量尝试和错误才意识到大多数ConfigurationManager引用都处理默认的appSettings,这与设置使用的applicationSettings不同。

I ultimately found the following:

我最终发现了以下内容:

Select the right ConfigurationSectionGroup/ConfigurationSectionClient, cast to SettingsSection, get the setting, and set the XML InnerText (e.g. below):

选择正确的ConfigurationSectionGroup / ConfigurationSectionClient,强制转换为SettingsSection,获取设置,并设置XML InnerText(例如下面):

// this gets the applicationSettings section (and the inner section 'inoBIBooks.My.MySettings')
Configuration config = WebConfigurationManager.OpenWebConfiguration("/" + targetvdir);
ConfigurationSectionGroup applicationSectionGroup = config.GetSectionGroup("applicationSettings");
ConfigurationSection applicationConfigSection = 
applicationSectionGroup.Sections["inoBIBooks.My.MySettings"];
ClientSettingsSection clientSection = (ClientSettingsSection)applicationConfigSection;

// set a value to that specific property
SettingElement applicationSetting = clientSection.Settings.Get("BIDB_Username");
applicationSetting.Value.ValueXml.InnerText = "username";

// without this, saving won't work
applicationConfigSection.SectionInformation.ForceSave = true;
// save
config.Save();

This is pulled from:
Access section 'applicationSettings' (not 'appSettings') in config file from setup
and
Save and reload app.config(applicationSettings) at runtime

这取自:设置中的配置文件中的访问部分'applicationSettings'(不是'appSettings')和运行时保存并重新加载app.config(applicationSettings)

#1


10  

If you change them to "user" settings they can be changed in code, and when you call Save() they will be saved to a user.config file in the current users local settings folder.

如果将它们更改为“用户”设置,则可以在代码中更改它们,当您调用Save()时,它们将保存到当前用户本地设置文件夹中的user.config文件中。

Obviously, this means they can be different for every user. Generally, global application settings that are the same for every user aren't modified in code because a change that one user makes will effect everyone else (hence the app settings are read only).

显然,这意味着每个用户可能会有所不同。通常,对于每个用户都相同的全局应用程序设置不会在代码中进行修改,因为一个用户所做的更改将影响其他所有人(因此应用程序设置是只读的)。

If you don't want them to be user scoped, take a look at the ConfigurationManager class. This will allow you to manually read and write to .config files. Remember though that the c:\program files\ folder is protected and normal users won't have access to it (this will result in UAC prompts or failure in vista/win7). Consider carefully how you will handle it, and remember that any change to an app.config will affect all users.

如果您不希望它们是用户范围的,请查看ConfigurationManager类。这将允许您手动读取和写入.config文件。请记住,c:\ program files \文件夹受到保护,普通用户将无法访问它(这将导致UAC提示或vista / win7失败)。仔细考虑如何处理它,并记住对app.config的任何更改都将影响所有用户。

There is no location in windows that all users are guaranteed to have write access to.

Windows中没有任何位置可以保证所有用户都具有写访问权限。

#2


0  

Look here: Best practice to save application settings in a Windows Forms Application

查看此处:在Windows窗体应用程序中保存应用程序设置的最佳实践

The ApplicationSettings class doesn't support saving settings to the app.config file. That's very much by design, apps that run with a properly secured user account (think Vista UAC) do not have write access to the program's installation folder.

ApplicationSettings类不支持将设置保存到app.config文件。这非常依赖于设计,使用适当安全的用户帐户(想想Vista UAC)运行的应用程序没有对程序安装文件夹的写入权限。

You can fight the system with the ConfigurationManager class. But the trivial workaround is to go into the Settings designer and change the setting's scope to User. If that causes hardships (say, the setting is relevant to every user), you should put your Options feature in a separate program so you can ask for the privilege elevation prompt. Or forego using a setting.

您可以使用ConfigurationManager类对抗系统。但是,简单的解决方法是进入“设置”设计器并将设置的范围更改为“用户”。如果这会导致困难(例如,设置与每个用户相关),则应将“选项”功能放在单独的程序中,以便可以请求权限提升提示。或放弃使用设置。

#3


0  

An old question, but I provide this answer to help anyone attempting to implement Simon P Stevens' answer related to the ConfigurationManager class since I wasn't sure how to do it being a novice with settings.

一个老问题,但我提供这个答案,以帮助任何人尝试实现Simon P Stevens与ConfigurationManager类相关的答案,因为我不知道如何做一个新手设置。

One of the first realizations was that the 2 Settings files in my C# project (made difficult because the typical one under Properties was there, but empty) were combined into the single .config and split between different ConfigurationSections. I thought that was why ConfigurationManager.AppSettings and ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None) both kept returning 0 keys.

最初的一个实现是我的C#项目中的2个设置文件(因为属性中的典型文件很难,但很空)被合并到单个.config中并在不同的ConfigurationSections之间拆分。我认为这就是为什么ConfigurationManager.AppSettings和ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)都保持返回0键的原因。

It took a lot of trial and error to realize that most of the ConfigurationManager references deal with the default appSettings and that was different than applicationSettings which is what Settings uses.

经过大量尝试和错误才意识到大多数ConfigurationManager引用都处理默认的appSettings,这与设置使用的applicationSettings不同。

I ultimately found the following:

我最终发现了以下内容:

Select the right ConfigurationSectionGroup/ConfigurationSectionClient, cast to SettingsSection, get the setting, and set the XML InnerText (e.g. below):

选择正确的ConfigurationSectionGroup / ConfigurationSectionClient,强制转换为SettingsSection,获取设置,并设置XML InnerText(例如下面):

// this gets the applicationSettings section (and the inner section 'inoBIBooks.My.MySettings')
Configuration config = WebConfigurationManager.OpenWebConfiguration("/" + targetvdir);
ConfigurationSectionGroup applicationSectionGroup = config.GetSectionGroup("applicationSettings");
ConfigurationSection applicationConfigSection = 
applicationSectionGroup.Sections["inoBIBooks.My.MySettings"];
ClientSettingsSection clientSection = (ClientSettingsSection)applicationConfigSection;

// set a value to that specific property
SettingElement applicationSetting = clientSection.Settings.Get("BIDB_Username");
applicationSetting.Value.ValueXml.InnerText = "username";

// without this, saving won't work
applicationConfigSection.SectionInformation.ForceSave = true;
// save
config.Save();

This is pulled from:
Access section 'applicationSettings' (not 'appSettings') in config file from setup
and
Save and reload app.config(applicationSettings) at runtime

这取自:设置中的配置文件中的访问部分'applicationSettings'(不是'appSettings')和运行时保存并重新加载app.config(applicationSettings)