如果*.exe.删除配置信息



我将应用程序设置添加到项目中。这些设置保存在'NameOfMyApp.exe.config'中。如果有人删除了这个文件会发生什么?有必要重新创建吗?我应该在哪里存储默认设置的值?

如果您正在使用设置设计器和生成的代码(在Properties名称空间中),则在(生成的)代码中具有默认值。

如果有人删除了。config文件,它就没了,宝贝,没了。并且需要再次创建、部署或获取它。

存储默认设置的值-可能有两个明显的答案是在配置文件或数据库表中。如果您的默认设置是相当静态的,并且不是特定于用户的,那么配置文件可能是一个不错的选择(或者在初始部署/安装时设置)。如果这些值可以被用户更改,那么最好将它们存储在数据库中。

我认为在哪里存储默认设置的答案取决于应用程序的性质以及用户如何使用它。

希望这对你有帮助!

UPDATE:哦,如果他们删除配置,我希望你把它存储在源代码管理的某个地方。可能会让你的生活轻松很多。祝你好运! !

程序将正常工作,如果有全局::System.Configuration。生成代码中的DefaultSettingValueAttribute

我写了一个简单的例子(Windows窗体):
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"]));
            }
        }
    }
} 

最新更新