settings file vs app.config [duplicate]

2020-05-24 20:40发布

问题:

This question already has answers here:
Closed 7 years ago.

Possible Duplicate:
What is the difference between app.config file and XYZ.settings file?

I'm not really understanding the interaction/differences between settings and config files. If I add an entry to the settings file, it gets added to the app.config as well. Does this mean that changing the value in the app.config will update the settings? If not, how do I update settings in a live application? What's the general purpose of using a settings file instead of putting things directly in app.config?

回答1:

From what I can tell by looking around, it seems the settings file and designer provide a sort of wrapper for the config file. The settings designer controls some values that go into the config file, but also modifies the .settings file in your Properties directory, and changes the generated code in your Settings.Designer.cs file.

Notice that in the config file, values don't have any types, but in the .settings file, they do. Now open Settings.Designer.cs. You'll see that the code generated there takes values out of your config file, and casts them to the proper types, as specified in your .settings file, or the settings designer. So it's providing a type safe interface to values in your config file. If you change a value in your config file, then the next time you go to the settings designer, it will ask you if you would like to update .settings to reflect those changes.

So to answer your question, once the app is deployed, you can change the value in the config file as long as it can be cast to the proper type, otherwise I'm guessing it will fail in Settings.Designer.cs. The only purpose that I can see seems to be type safety and convenience of a designer over handling the XML of the config file.

Also as an aside, remember that because config is not a versioned file, installers will not update them on a client's machine. It won't allow it to stomp on any user changes. So once it's deployed, if you put your own changes into the config that users will need, they will have to copy the changes over manually.