user2550641
user2550641

Reputation: 347

File Watcher in VSPackage

I'm new in developing in Visual Studio so I hope my problem is not too stupid :-) I try to establish a FileWatcher in my VSPackage to detect any changes performed in the current solution. For this I found the class FileWatcher, which - in my opinion - detect any file changes. But anything in my code must be wrong as no event is fired. Maybe you can help me?

The solution I want to watch is in D:\\Test but entering the specific path doesn't help

class Watcher
{
    FileSystemWatcher watcher = new FileSystemWatcher();

    [PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
    public void StartWatching()
    {
        Debug.WriteLine("in watcher method");
        watcher.Path = @"D:\\"; 
        watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
           | NotifyFilters.FileName | NotifyFilters.DirectoryName;

        watcher.Changed += new FileSystemEventHandler(OnChanged);
        watcher.Created += new FileSystemEventHandler(OnChanged);
        watcher.Deleted += new FileSystemEventHandler(OnChanged);
        watcher.Renamed += new RenamedEventHandler(OnRenamed);

        watcher.EnableRaisingEvents = true;
    }

    private static void OnChanged(object source, FileSystemEventArgs e)
    {
        Debug.WriteLine("Changes in folder:" + e.FullPath);
    }

    private static void OnRenamed(object source, RenamedEventArgs e)
    {
        Debug.WriteLine("File: {0} renamed to {1}", e.OldFullPath, e.FullPath);
    }

    private static void OnDeleted(object source, RenamedEventArgs e)
    {
        Debug.WriteLine("deleted");
    }

    private static void OnCreated(object source, RenamedEventArgs e)
    {
        Debug.WriteLine("created");
    }
}

In the initialize method of my VSPackage i call: ("in watcher method" is displayed in output so this part should work)

 watch = new Watcher();
        watch.StartWatching();

Tank you in advance!

EDIT

I found out, that the path at the test environment changes. But now I have the problem, that I'm not able to get the name of the changed file, as the change event only gives me .opensdf or .sdf

Can you help me?

Upvotes: 1

Views: 310

Answers (1)

Frank
Frank

Reputation: 2198

Instead of watching the file changes yourself you can use the built in events provided by Visual Studio: SolutionEvents and ProjectItemEvents. This Stack Overflow post explains how to use them in a vs package.

Upvotes: 1

Related Questions