سؤال

I was just testing the functionality of FileSystemWatcher when I got ArguementException on this line

    fileSystemWatcher1.Path = Environment.SpecialFolder.MyComputer.ToString() ; 

Why is this error coming, if the given path is valid. The directory name MyComputer is invalid.

هل كانت مفيدة؟

المحلول

Environment.SpecialFolder is an enumeration. When you call .ToString() on an enum value, you get the string representation of that enum value - literally MyComputer in this case. If you want to get the location of the MyComputer folder, you want to use Environment.GetFolderPath.

However, MyComputer will return an empty string, so you still can't use it in the way you're attempting.

نصائح أخرى

"I want to watch all the drives."

I'm not sure that you will be able to implement the FileSystemWatcher in a way that is global for all connected storage in this way.

"MyComputer" is not a physical location and so can not be used as an entry point in the way described.

If you really REALLY want to watch all files on all drives you can do something like this:

        var w = Environment.GetLogicalDrives().Select(path => new FileSystemWatcher(path)).ToList();

        foreach (var watcher in w)
        {
            watcher.Changed+=watcher_Changed;
        }

Where the watcher_Changed is the event handler for each of the FileSystemWatchers.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top