English 中文(简体)
文件系统监视器无法启动
原标题:FileSystemWatcher unable to start

我正在开发一个使用 C# 2010 的 Windows Service, 使用 FileSystem 观察器。 出于某种原因, 我无法开始我的服务 。

当我试图履行我的职责时,我收到以下信息。

- 本地计算机上的服务开始和停止,有些服务如果未用于其他服务或程序,则自动启动。

当我把这个代码拼起来时, 就会发生这种情况:

启动 :

FSWatcherTest.Path = ConfigurationManager.AppSettings["WatchPath"].ToString();

然后在一种方法中:

private void InitializeComponent()
{
     this.FSWatcherTest = new FileSystemWatcher();
     ((System.ComponentModel.ISupportInitialize)(this.FSWatcherTest)).BeginInit();

       this.FSWatcherTest.EnableRaisingEvents = true;
       this.FSWatcherTest.Path = "my path";
       this.FSWatcherTest.Changed += new      FileSystemEventHandler(this.FSWatcherTest_Changed_1);
       ((ISupportInitialize)(this.FSWatcherTest)).EndInit();

}

等待一些可能的答案 提前感谢!

问题回答

这可能是一个安全问题,因为文件系统监视器需要完全信任

我很久没用过这个了 所以我可能不在基地了 但你可能在文件系统观察器准备好之前 就打开它了

在上文的代码中, 初始化( 作为设计师生成的代码的一部分, 我猜测) 将“ 启用时间” 设置为真的 。 设计师默认该属性, 以便您在设计时也设定一个有效的路径。 但是您没有在设计时设定一个真实路径( 您只是使用“ 我路径 ” ) 。 FSW 可能在 EndInit () 之后丢出一个例外, 因为它开始寻找“ 我路径”, 却找不到它 。

自您在 OnStart 方法中重新设定真实路径以来, 您应该在设计师中设置假的 Events 启用。 在设置路径后, 在 OnStart 方法中设置为真 。 这是您想要它开始寻找更改的时候 。





相关问题
Manually implementing high performance algorithms in .NET

As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...

Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How do I compare two decimals to 10 decimal places?

I m using decimal type (.net), and I want to see if two numbers are equal. But I only want to be accurate to 10 decimal places. For example take these three numbers. I want them all to be equal. 0....

Exception practices when creating a SynchronizationContext?

I m creating an STA version of the SynchronizationContext for use in Windows Workflow 4.0. I m wondering what to do about exceptions when Post-ing callbacks. The SynchronizationContext can be used ...

Show running instance in single instance application

I am building an application with C#. I managed to turn this into a single instance application by checking if the same process is already running. Process[] pname = Process.GetProcessesByName("...

How to combine DataTrigger and EventTrigger?

NOTE I have asked the related question (with an accepted answer): How to combine DataTrigger and Trigger? I think I need to combine an EventTrigger and a DataTrigger to achieve what I m after: when ...

热门标签