为什么app.config中的应用程序设置是只读的?

时间:2022-09-23 14:11:46

I have some settings in my app.config which I intend to be 'global' - ie. any user can change them, and all users get the same setting.

我在app.config中有一些设置,我打算成为'全局' - 即。任何用户都可以更改它们,所有用户都可以获得相同的设置。

But unless I change them to be user settings, they are read only.

但除非我将它们更改为用户设置,否则它们是只读的。

Why is this?

为什么是这样?

And how should I go about persisting my app's global settings?

我应该如何坚持我的应用程序的全局设置?

Edit:

编辑:

This is actually a windows service application which runs as a service as LocalSystem. It can also be run manually by a local admin with argument "/config", which launches a windows form to edit configuration values.

这实际上是一个Windows服务应用程序,它作为LocalSystem服务运行。它也可以由具有参数“/ config”的本地管理员手动运行,它启动一个窗口表单来编辑配置值。

So it will have write access to %PROGRAMFILES% in both situations.

因此,在两种情况下都可以对%PROGRAMFILES%进行写访问。

The way I am accessing my settings is thusly:

因此,我访问我的设置的方式是:

Settings.Default.MySetting = MyNewValue;

And when MySetting is set to Application (in my project properties, Settings.settings), I get a compile-time error "MySetting is read only".

当MySetting设置为Application(在我的项目属性Settings.settings中)时,我收到编译时错误“MySetting is read only”。

I am new to this stuff, and have not yet found a very good explanation of how it is supposed to be done. For example, why do I need to say 'Default', and what does that actually mean? I have no idea. If anyone can point me to an app.config usage tutorial, that would be really helpful.

我对这些东西不熟悉,还没有找到一个很好的解释它应该如何完成。例如,为什么我需要说“默认”,这究竟意味着什么?我不知道。如果有人能指出我的app.config使用教程,那将非常有帮助。

6 个解决方案

#1


52  

The real complete answer:

真正完整的答案:

The app.config settings are read-only because there are 2 types of settings:

app.config设置是只读的,因为有两种类型的设置:

  1. Application Settings
  2. 应用程序设置
  3. User Settings
  4. 用户设置

The first won't change unless the application publisher publishes a new version of it. The second is not stored in the app.config, but in a user.config file. In the abscence of this user.config file the app.config provides the default value.

除非应用程序发布者发布新版本,否则第一个不会更改。第二个不存储在app.config中,而是存储在user.config文件中。在此user.config文件的abscence中,app.config提供了默认值。

If MySetting is a User Setting:

如果MySetting是用户设置:

Settings.Default.MySetting = MyNewValue;
Settings.Default.Save();

It will create a user.config file at [User Local Settings Application Data]\[company name]\[application].exe[hash string]\[version] with the new settings, and those settings will prevail over the settings in the app.config file.

它将在[用户本地设置应用程序数据] \ [公司名称] \ [应用程序] .exe [哈希字符串] \ [版本]中使用新设置创建user.config文件,这些设置将优先于app.config文件。

#2


4  

Why: Application settings are intended to be stored in the Application folder under Program Files where the user does not have write privileges.

原因:应用程序设置旨在存储在用户没有写入权限的Program Files下的Application文件夹中。

How: There is no default support for "All Users" but you should be able to setup your own custom config file in a public folder or use a Database.

方法:“所有用户”没有默认支持,但您应该能够在公共文件夹中设置自己的自定义配置文件或使用数据库。

#3


3  

Simply put: There's no location on a machine that everyone can change, unless you give privileges to do so.

简单地说:除非你给予特权,否则机器上没有任何人可以更改的位置。

There are several ways to deal with this kind of situation:

有几种方法可以解决这种情况:

  • You can create a configuration file / some registry settings, put this in the "all users" profile and grant "Everyone" the rights to change that specific file. During installation you can automate the procedure for granting the appropiate privileges and your program can handle the rest.

    您可以创建配置文件/某些注册表设置,将其放在“所有用户”配置文件中,并授予“Everyone”更改该特定文件的权限。在安装过程中,您可以自动执行授予适当权限的过程,您的程序可以处理其余的权限。

  • You can leverage UAC to make sure the current user has the appropiate privileges to change a system-wide setting. This is the recommended approach but also means that not everyone can change specific settings.

    您可以利用UAC确保当前用户具有相应的权限来更改系统范围的设置。这是推荐的方法,但也意味着不是每个人都可以更改特定设置。

  • You can use a shared database and store your settings in there.

    您可以使用共享数据库并将设置存储在那里。

  • ???

    ???

I would not recommend to change items in the program files directory or changing the default privileges overthere.

我不建议更改程序文件目录中的项目或更改默认权限。

EDIT: As local system you have indeed write privileges to the program files directory. If you get the "Read only" error, it means the settings itself are read only. You'll need to use the configuration manager to be able to change the settings in configuration files.

编辑:作为本地系统,您确实已将权限写入程序文件目录。如果出现“只读”错误,则表示设置本身是只读的。您需要使用配置管理器才能更改配置文件中的设置。

Hope this helps.

希望这可以帮助。

#4


1  

Not quite sure what you mean here. Do you mean you allowed users to alter app.config from the UI and the changes are not persisted?

不太清楚你的意思。您是说您允许用户从UI更改app.config并且更改不会保留?

did you call

你打电话了吗?

ConfigurationManager.RefreshSection("appSettings");

and

Configuration.Save();

#5


0  

One reason is that the app.config file is in your app's folder under the Program Files directory, and everything in Program Files is read only for standard users by default.

一个原因是app.config文件位于Program Files目录下的应用程序文件夹中,默认情况下,Program Files中的所有内容都只读标准用户。

Another is that app.config settings apply system wide. If one user makes a change it will impact other users. Normal users are not supposed to be able to make that kind of change. Anything that can impact multiple users should be set only a system administrator. Per-user settings belong in each user's Application Data folder.

另一个是app.config设置适用于系统范围。如果一个用户进行更改,则会影响其他用户。普通用户不应该做出那种改变。任何可能影响多个用户的事情都应该只设置系统管理员。每用户设置属于每个用户的Application Data文件夹。

#6


0  

Configuration Settings are cached in the memory when you starts the application. you can deal with the app.config file as xml to change the values.

启动应用程序时,配置设置将缓存在内存中。您可以将app.config文件作为xml处理以更改值。

#1


52  

The real complete answer:

真正完整的答案:

The app.config settings are read-only because there are 2 types of settings:

app.config设置是只读的,因为有两种类型的设置:

  1. Application Settings
  2. 应用程序设置
  3. User Settings
  4. 用户设置

The first won't change unless the application publisher publishes a new version of it. The second is not stored in the app.config, but in a user.config file. In the abscence of this user.config file the app.config provides the default value.

除非应用程序发布者发布新版本,否则第一个不会更改。第二个不存储在app.config中,而是存储在user.config文件中。在此user.config文件的abscence中,app.config提供了默认值。

If MySetting is a User Setting:

如果MySetting是用户设置:

Settings.Default.MySetting = MyNewValue;
Settings.Default.Save();

It will create a user.config file at [User Local Settings Application Data]\[company name]\[application].exe[hash string]\[version] with the new settings, and those settings will prevail over the settings in the app.config file.

它将在[用户本地设置应用程序数据] \ [公司名称] \ [应用程序] .exe [哈希字符串] \ [版本]中使用新设置创建user.config文件,这些设置将优先于app.config文件。

#2


4  

Why: Application settings are intended to be stored in the Application folder under Program Files where the user does not have write privileges.

原因:应用程序设置旨在存储在用户没有写入权限的Program Files下的Application文件夹中。

How: There is no default support for "All Users" but you should be able to setup your own custom config file in a public folder or use a Database.

方法:“所有用户”没有默认支持,但您应该能够在公共文件夹中设置自己的自定义配置文件或使用数据库。

#3


3  

Simply put: There's no location on a machine that everyone can change, unless you give privileges to do so.

简单地说:除非你给予特权,否则机器上没有任何人可以更改的位置。

There are several ways to deal with this kind of situation:

有几种方法可以解决这种情况:

  • You can create a configuration file / some registry settings, put this in the "all users" profile and grant "Everyone" the rights to change that specific file. During installation you can automate the procedure for granting the appropiate privileges and your program can handle the rest.

    您可以创建配置文件/某些注册表设置,将其放在“所有用户”配置文件中,并授予“Everyone”更改该特定文件的权限。在安装过程中,您可以自动执行授予适当权限的过程,您的程序可以处理其余的权限。

  • You can leverage UAC to make sure the current user has the appropiate privileges to change a system-wide setting. This is the recommended approach but also means that not everyone can change specific settings.

    您可以利用UAC确保当前用户具有相应的权限来更改系统范围的设置。这是推荐的方法,但也意味着不是每个人都可以更改特定设置。

  • You can use a shared database and store your settings in there.

    您可以使用共享数据库并将设置存储在那里。

  • ???

    ???

I would not recommend to change items in the program files directory or changing the default privileges overthere.

我不建议更改程序文件目录中的项目或更改默认权限。

EDIT: As local system you have indeed write privileges to the program files directory. If you get the "Read only" error, it means the settings itself are read only. You'll need to use the configuration manager to be able to change the settings in configuration files.

编辑:作为本地系统,您确实已将权限写入程序文件目录。如果出现“只读”错误,则表示设置本身是只读的。您需要使用配置管理器才能更改配置文件中的设置。

Hope this helps.

希望这可以帮助。

#4


1  

Not quite sure what you mean here. Do you mean you allowed users to alter app.config from the UI and the changes are not persisted?

不太清楚你的意思。您是说您允许用户从UI更改app.config并且更改不会保留?

did you call

你打电话了吗?

ConfigurationManager.RefreshSection("appSettings");

and

Configuration.Save();

#5


0  

One reason is that the app.config file is in your app's folder under the Program Files directory, and everything in Program Files is read only for standard users by default.

一个原因是app.config文件位于Program Files目录下的应用程序文件夹中,默认情况下,Program Files中的所有内容都只读标准用户。

Another is that app.config settings apply system wide. If one user makes a change it will impact other users. Normal users are not supposed to be able to make that kind of change. Anything that can impact multiple users should be set only a system administrator. Per-user settings belong in each user's Application Data folder.

另一个是app.config设置适用于系统范围。如果一个用户进行更改,则会影响其他用户。普通用户不应该做出那种改变。任何可能影响多个用户的事情都应该只设置系统管理员。每用户设置属于每个用户的Application Data文件夹。

#6


0  

Configuration Settings are cached in the memory when you starts the application. you can deal with the app.config file as xml to change the values.

启动应用程序时,配置设置将缓存在内存中。您可以将app.config文件作为xml处理以更改值。