如何检测设置文件的 属性 值是否已更改?
How to detect a property value of the setting file is changed?
我试过做一个简单的用户授权。 安装后,Settings1 文件中名为“UserHasBeenVerified”的 属性 已更改。 我已将 Settings Changed 事件处理程序添加到 App.cs 文件,但我仍然没有不知道原因。 我希望 属性 在安装后为 FALSE。但不知何故它确实是真的。
你觉得我该如何解决这个问题?谢谢。
我创建了一个安装程序 Class 以在安装后重置 Settings1:
[RunInstaller(true)]
public partial class InstallerClass : System.Configuration.Install.Installer
{
public InstallerClass()
{
InitializeComponent();
}
protected override void OnAfterInstall(IDictionary savedState)
{
// This function runs when the installation is finished.
base.OnAfterInstall(savedState);
try
{
System.Windows.MessageBox.Show($"01\nVERIFIED STATE: {BackendLayer.Settings1.Default.UserHasBeenVerified}"); // Shows: VERIFIED STATE: False
BackendLayer.Settings1.Default.Reset();
System.Windows.MessageBox.Show($"02\nVERIFIED STATE: {BackendLayer.Settings1.Default.UserHasBeenVerified}"); // Shows: VERIFIED STATE: False
BackendLayer.Settings1.Default.Save();
System.Windows.MessageBox.Show($"03\nVERIFIED STATE: {BackendLayer.Settings1.Default.UserHasBeenVerified}"); // Shows: VERIFIED STATE: False
System.Windows.MessageBox.Show("Installed");
}
catch
{
System.Windows.MessageBox.Show("NOT Installed");
}
}
}
App.cs 文件是:
App()
{
// Runs first whenever the program is opened
BackendLayer.Settings1.Default.SettingChanging += Default_SettingChanging;
MessageBox.Show($"VERIFIED STATE: {BackendLayer.Settings1.Default.UserHasBeenVerified}"); // Shows: VERIFIED STATE: TRUE
}
private void Default_SettingChanging(object sender, System.Configuration.SettingChangingEventArgs e)
{
// Fires when any property in setting1 is changed.
MessageBox.Show($"Setting Name: {e.SettingName}\nSetting Key: {e.SettingKey}\nSetting Class: {e.SettingClass}\nNew value: {e.NewValue}");
}
调用App()构造函数的函数:
/// <summary>
/// Application Entry Point.
/// </summary>
[System.STAThreadAttribute()]
[System.Diagnostics.DebuggerNonUserCodeAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("PresentationBuildTasks", "4.0.0.0")]
public static void Main() {
ScheduleProgram.App app = new ScheduleProgram.App();
app.InitializeComponent();
app.Run();
}
Settings1.Designer.cs 中的 UserHasBeenVerified 代码:
[global::System.Configuration.UserScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Configuration.DefaultSettingValueAttribute("False")]
public bool UserHasBeenVerified {
get {
return ((bool)(this["UserHasBeenVerified"]));
}
set {
this["UserHasBeenVerified"] = value;
}
}
程序按顺序安装后的输出:
- 01\nVERIFIED 状态:错误
- 02\nVERIFIED 状态:错误
- 03\nVERIFIED 状态:错误
- 已安装
程序打开后的输出:
- 已验证状态:正确
我尝试过的东西:
我已经更改了 Settings1 文件的这些属性:
*Build Action: From "None" to "Content"* ,
*Copy to Output Directory: From "Do not copy" to "Copy always"*
我不认为 BackendLayer.Settings1.Default
在安装程序中调用它时从文件中获取值,您是从 Settings1 class 实例化的实例中获取值默认构造函数。
确认 UserHasBeenVerified
的值在您的 Settings1.settings
文件中是正确的,并且在您构建应用程序时将其复制到您的 app.config
。由于您只需要默认值,您的安装程序只需要打包构建应用程序的输出
我试过做一个简单的用户授权。 安装后,Settings1 文件中名为“UserHasBeenVerified”的 属性 已更改。 我已将 Settings Changed 事件处理程序添加到 App.cs 文件,但我仍然没有不知道原因。 我希望 属性 在安装后为 FALSE。但不知何故它确实是真的。
你觉得我该如何解决这个问题?谢谢。
我创建了一个安装程序 Class 以在安装后重置 Settings1:
[RunInstaller(true)]
public partial class InstallerClass : System.Configuration.Install.Installer
{
public InstallerClass()
{
InitializeComponent();
}
protected override void OnAfterInstall(IDictionary savedState)
{
// This function runs when the installation is finished.
base.OnAfterInstall(savedState);
try
{
System.Windows.MessageBox.Show($"01\nVERIFIED STATE: {BackendLayer.Settings1.Default.UserHasBeenVerified}"); // Shows: VERIFIED STATE: False
BackendLayer.Settings1.Default.Reset();
System.Windows.MessageBox.Show($"02\nVERIFIED STATE: {BackendLayer.Settings1.Default.UserHasBeenVerified}"); // Shows: VERIFIED STATE: False
BackendLayer.Settings1.Default.Save();
System.Windows.MessageBox.Show($"03\nVERIFIED STATE: {BackendLayer.Settings1.Default.UserHasBeenVerified}"); // Shows: VERIFIED STATE: False
System.Windows.MessageBox.Show("Installed");
}
catch
{
System.Windows.MessageBox.Show("NOT Installed");
}
}
}
App.cs 文件是:
App()
{
// Runs first whenever the program is opened
BackendLayer.Settings1.Default.SettingChanging += Default_SettingChanging;
MessageBox.Show($"VERIFIED STATE: {BackendLayer.Settings1.Default.UserHasBeenVerified}"); // Shows: VERIFIED STATE: TRUE
}
private void Default_SettingChanging(object sender, System.Configuration.SettingChangingEventArgs e)
{
// Fires when any property in setting1 is changed.
MessageBox.Show($"Setting Name: {e.SettingName}\nSetting Key: {e.SettingKey}\nSetting Class: {e.SettingClass}\nNew value: {e.NewValue}");
}
调用App()构造函数的函数:
/// <summary>
/// Application Entry Point.
/// </summary>
[System.STAThreadAttribute()]
[System.Diagnostics.DebuggerNonUserCodeAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("PresentationBuildTasks", "4.0.0.0")]
public static void Main() {
ScheduleProgram.App app = new ScheduleProgram.App();
app.InitializeComponent();
app.Run();
}
Settings1.Designer.cs 中的 UserHasBeenVerified 代码:
[global::System.Configuration.UserScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Configuration.DefaultSettingValueAttribute("False")]
public bool UserHasBeenVerified {
get {
return ((bool)(this["UserHasBeenVerified"]));
}
set {
this["UserHasBeenVerified"] = value;
}
}
程序按顺序安装后的输出:
- 01\nVERIFIED 状态:错误
- 02\nVERIFIED 状态:错误
- 03\nVERIFIED 状态:错误
- 已安装
程序打开后的输出:
- 已验证状态:正确
我尝试过的东西:
我已经更改了 Settings1 文件的这些属性:
*Build Action: From "None" to "Content"* ,
*Copy to Output Directory: From "Do not copy" to "Copy always"*
我不认为 BackendLayer.Settings1.Default
在安装程序中调用它时从文件中获取值,您是从 Settings1 class 实例化的实例中获取值默认构造函数。
确认 UserHasBeenVerified
的值在您的 Settings1.settings
文件中是正确的,并且在您构建应用程序时将其复制到您的 app.config
。由于您只需要默认值,您的安装程序只需要打包构建应用程序的输出