问题
I have a large config file (user) that i needed to go to the right location and have some default values.
Since i have a installer class, i added some parameter setting to the config file in it, but created the config files in the installers folder.
What is the best way to ensure these default parameters will be written only once, and in the right location?
回答1:
A standard way of using defaults in the app.config. For example, here's a default value per version I use to ensure the code copies user settings between upgrades:
<userSettings>
<Software.Namespace.Properties.UserSettings>
<setting name="RequiresUpgrade" serializeAs="String">
<value>True</value>
</setting>
</Software.Namespace.Properties.UserSettings>
</userSettings>
<-- .... -->
<userSettings>
<Software.Namespace.Properties.UserSettings>
<setting name="RequiresUpgrade" serializeAs="String">
<value>True</value>
</setting>
</Software.Namespace.Properties.UserSettings>
</userSettings>
</configuration>
You will need to add a .settings file to your project, or go to your project properties -> Settings and configure them from there.
The location of the user's own settings file is usually placed within their AppSettings folder in their profile. I'm not sure if this can be changed, but I seem to remember reading out it.
EDIT
There's some more information about it here: Application Settings Architecture
It also shows the following example if you want to keep it simple:
[UserScopedSetting()]
[DefaultSettingValue("white")]
public Color BackgroundColor
{
get
{
return ((Color)this["BackgroundColor"]);
}
set
{
this["BackgroundColor"] = (Color)value;
}
}
And this looks like it's very important to note when using this (quote):
For a Windows Forms-based application copied onto the local computer, app.exe.config will reside in the same directory as the base directory of the application's main executable file, and user.config will reside in the location specified by the Application.LocalUserAppDataPath property. For an application installed by means of ClickOnce, both of these files will reside in the ClickOnce Data Directory underneath %InstallRoot%\Documents and Settings\username\Local Settings.
The storage location of these files is slightly different if a user has enabled roaming profiles, which enables a user to define different Windows and application settings when he or she is using other computers within a domain. In that case, both ClickOnce applications and non-ClickOnce applications will have their app.exe.config and user.config files stored under %InstallRoot%\Documents and Settings\username\Application Data.
来源:https://stackoverflow.com/questions/2515146/again-user-config-files-c-sharp