1

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?

James Webster
  • 31,873
  • 11
  • 70
  • 114
user2550641
  • 317
  • 4
  • 18

1 Answers1

1

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.

Community
  • 1
  • 1
Frank
  • 2,178
  • 2
  • 17
  • 24
  • Hey Frank, thank you for your answer! It took some time, but now my solution/projectitem events are triggered :-) – user2550641 May 12 '15 at 07:23
  • Glad that worked for you, if you are new to vsix development I suggest you check out this [excellent series of posts](https://learnvsxnow.codeplex.com/). The information references VS 2010 but most of it still applies to newer versions. I found it extremely helpful when I first started extension development. The link starts at part 16 but if you scroll to the very bottom there is another link to parts 1 - 15. – Frank May 12 '15 at 13:23