Click here to Skip to main content
15,896,118 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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++;
       }
}

Any solution would get my great appreciation.
Thanks.. :doh:
Posted
Updated 10-Aug-10 2:57am
v2
Comments
Sandeep Mewara 10-Aug-10 8:57am    
Just formatted the code part. Use PRE tags to format code from next time.

1 solution

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.

C#
List<FileSystemWatcher> watchers = new List<FileSystemWatcher>()
 
Share this answer
 
v2
Comments
bonypatel 10-Aug-10 9:42am    
Hi,
Above code is working properly in Local system, But the problem is I can't found other lan machine hard drive path.

This tool services install in all lan machine.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900