I added application settings to t开发者_如何学运维he project. These settings are saved in 'NameOfMyApp.exe.config'. What would happen if somebody were to delete this file? Is it necessary to create it again? Where should I store the values of default settings?
If you are using the settings designer and the generated code (in the Properties namespace) you have defaults in the (generated) code.
If someone deletes the .config file, it's gone, baby, gone. And it will be necessary to create, deploy, or GET it again.
Storing values of default settings - probably two obvious answers would be in a config file or a database table. If you default settings are fairly static and not really user specific, a config file may be a good option (or are set at the initial deployment/installation). If these values can change by the user, it's probably better to store them in the database.
I think the answer to where to store your default settings really depends on the nature of the application and how the user's use it.
Hope this helps!
UPDATE: oh, and if they do delete the config, I hope you have it stored in source control somewhere. :) Probably make your life a lot easier. Good luck!!
Program will be work correctly if there is global::System.Configuration.DefaultSettingValueAttribute in generated code.
I wrote simple example (Windows Forms):
using System;
using System.Windows.Forms;
namespace AppSettings {
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e) {
textBox1.Text = Properties.Settings.Default.tbText;
}
}
}
TbText scope is application. I compiled it in release mode. I deleted all files except *.exe. It is work correct because this setting is into assembly:
namespace AppSettings.Properties {
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "10.0.0.0")]
internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase {
private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings())));
public static Settings Default {
get {
return defaultInstance;
}
}
[global::System.Configuration.ApplicationScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
//here!
[global::System.Configuration.DefaultSettingValueAttribute("!!!***HALLO***!!!")]
//
public string tbText {
get {
return ((string)(this["tbText"]));
}
}
}
}
精彩评论