[英]What will happen if *.exe.config is deleted?
我在項目中添加了應用程序設置。 這些設置保存在“NameOfMyApp.exe.config”中。 如果有人要刪除這個文件會怎么樣? 是否有必要再次創建它? 我應該在哪里存儲默認設置的值?
如果有人刪除.config文件,它已經消失了,寶貝,走了。 並且有必要再次創建,部署或獲取它。
存儲默認設置的值 - 可能有兩個明顯的答案是配置文件或數據庫表。 如果默認設置是相當靜態的而不是真正特定於用戶的,則配置文件可能是一個不錯的選擇(或者在初始部署/安裝時設置)。 如果用戶可以更改這些值,則最好將它們存儲在數據庫中。
我認為存儲默認設置的位置的答案實際上取決於應用程序的性質以及用戶如何使用它。
希望這可以幫助!
更新:哦,如果他們確實刪除了配置,我希望你把它存儲在某個地方的源代碼管理中。 :)可能會讓你的生活更輕松。 祝好運!!
如果您使用的是設置設計器和生成的代碼(在“屬性”命名空間中),則在(生成的)代碼中有默認值。
如果生成的代碼中存在global :: System.Configuration.DefaultSettingValueAttribute,則程序將正常工作。
我寫了一個簡單的例子(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范圍是應用程序。 我在發布模式下編譯它。 我刪除了除* .exe以外的所有文件。 這是正確的,因為此設置是匯編:
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"]));
}
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.