gpt4 book ai didi

c# - 每次我打开 Visual Studio 时,FileSystemWatcher EnableRaisingEvent 都会发生变化

转载 作者:太空狗 更新时间:2023-10-29 23:24:19 25 4
gpt4 key购买 nike

我在带有框架 4.0 的 Visual Studio 2010 中使用 C#。

在我的项目中,以两种不同的形式,有两个 FileSystemWatcher,其属性 EnableRaisingEvent 设置为 false。如果我关闭 Visual Studio,当我重新打开它时,我会在 FileSystemWatcher 中将属性 EnableRaisingEvent 设置为 true

在设计器文件中我的两个表单中都有以下代码:

private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));
this.fileSystemWatcher1 = new System.IO.FileSystemWatcher();
((System.ComponentModel.ISupportInitialize)(this.fileSystemWatcher1)).BeginInit();
this.SuspendLayout();

this.fileSystemWatcher1.Filter = "my_filter";
this.fileSystemWatcher1.NotifyFilter = System.IO.NotifyFilters.LastWrite;
this.fileSystemWatcher1.SynchronizingObject = this;
this.fileSystemWatcher1.Changed += new System.IO.FileSystemEventHandler(this.fileSystemWatcher1_Changed);
}

未设置属性 EnableRaisingEvent,但默认值为 false

知道为什么我会出现这种奇怪的行为吗?

编辑

我听从了 Virtlink 的建议,添加了以下代码行:

this.fileSystemWatcher1.EnableRaisingEvents = false;

它似乎解决了我的问题,但几天后(以及一些项目的打开、关闭和重建,但没有修改 fileSystemWatcher1)我发现:

  • 在设计器中,fileSystemWatcher1 的属性中,EnableRaisingEvents 已设置回 true

    <
  • 在代码中,之前添加的行丢失了

我尝试迁移到 Visual Studio 2012(仍然是框架 4.0),解决方法解决了这个问题多几天。然后我遇到了和VS10一样的情况。

还有其他想法吗?

最佳答案

它也发生在 Visual Studio 2012 中,您不必关闭 Visual Studio。重新打开表单设计器足以将属性设置为 True,无论是在设计器中还是在运行时都是如此。

这似乎是 FileSystemWatcher 中的错误。

解决方法是将此行显式添加到您的 InitializeComponent:

this.fileSystemWatcher1.EnableRaisingEvents = false;

如果设计师不与您合作,您将不得不反对它。您放入 InitializeComponent 中的任何内容都可能被设计者覆盖或删除,因为 InitializeComponent 是设计者的领地。处理此问题的一种方法是在窗体的构造函数中 调用 InitializeComponent 之后添加这一行。

InitializeComponent();
this.fileSystemWatcher1.EnableRaisingEvents = false;

这意味着设计人员将不会向您显示 EnableRaisingEvents 的正确值,但由于无论如何它都不能很好地工作,所以可能不是什么大问题.将此行放在构造函数中可确保它不会在将来的任何时候被设计人员删除。

关于c# - 每次我打开 Visual Studio 时,FileSystemWatcher EnableRaisingEvent 都会发生变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14985786/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com