FileSystemWatcher在LAN上无法使用 [英] FileSystemWatcher can not work on LAN

查看:83
本文介绍了FileSystemWatcher在LAN上无法使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我正在为所有硬盘目录创建Monitor LOG的工具.我使用Filesystemwatcher 使用C#制作此应用程序.

我在局域网中使用此工具.并将日志保存在所有局域网计算机的一台服务器上的SqlServer-2005数据库中.

问题是,此工具可以在我的本地计算机上正常工作,但是不能在任何LAN机器上工作.

我用唯一的ConnectionString连接Sql服务器. 数据源= NCT120;初始目录= FILE_LOGGER;集成安全性= True"

我将以下代码用于onstart服务...

Hi all,

I am making tools for Monitor LOG for all hard drive directory. I using Filesystemwatcher for making this application with C#.

I use this tools in LAN. And save log in SqlServer-2005 database on one server for all LAN machine.

The problem is that, this tools work in my local machine properly, But cant work in any LAN machine.

I connect Sql server with unique ConnectionString. "Data Source=NCT120;Initial Catalog=FILE_LOGGER;Integrated Security=True"

I use below code for onstart services...

protected override void OnStart(string[] args)
{
	string[] drives = Environment.GetLogicalDrives();
        _watchers = new FileSystemWatcher[drivers.Length];
        int i = 0;
        foreach (string strDrive in drives)
        {
           FileSystemWatcher _watcher = new FileSystemWatcher();
           _watcher.Path = strDrive;
           _watcher.Changed += new FileSystemEventHandler	(FolderWatcherTest_Changed);

           _watcher.Created += new FileSystemEventHandler	(FolderWatcherTest_Created);

           _watcher.Deleted += new FileSystemEventHandler (FolderWatcherTest_Deleted);

           _watcher.Renamed += new RenamedEventHandler(FolderWatcherTest_Renamed);

          _watchres[i] = watcher;

          '' Begin watching.
          watcher.EnableRaisingEvents = true;
          i++;
       }
}


任何解决方案都会令我感激不尽.
谢谢..:doh:


Any solution would get my great appreciation.
Thanks.. :doh:

推荐答案

好吧,您的FileSystemWatcher对象受到其范围的限制.换句话说,您在foreach循环中定义了对象,它之所以会蹲下是因为退出当前迭代时,它超出了范围.

通过在CLASS中定义以下对象(而不是在方法中定义)来重构代码.

well, your FileSystemWatcher object is constrained by its scope. In other words, you defined the object INSIDE the foreach loop, and it ain''d gonna do squat because when you exite the current iteration, it goes out of scope.

Refactor your code by defining the following object in your CLASS as opposed to being defined in the method.

List<FileSystemWatcher> watchers = new List<FileSystemWatcher>()


这篇关于FileSystemWatcher在LAN上无法使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆