Click here to Skip to main content
15,898,035 members
Please Sign up or sign in to vote.
1.80/5 (2 votes)
See more:
I want develop photo screen saver same as windows has.Problem with windows photo screen saver is that if anything is added to photo folder .It didnt get updated
till we restart it.I want develop application which will get notified about changes without restarting it.How Can I achieve this??
tried with file watcher option didnt work.
C#
class Watcher
   {
       [PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
       public static void Run(string path)
       {

           // Create a new FileSystemWatcher and set its properties.
           FileSystemWatcher watcher = new FileSystemWatcher();
           watcher.Path = path;
           /* Watch for changes in LastAccess and LastWrite times, and
              the renaming of files or directories. */
           watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
              | NotifyFilters.FileName | NotifyFilters.DirectoryName;
           // Only watch text files.
           watcher.Filter = "*.txt";

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

           // Begin watching.
           watcher.EnableRaisingEvents = true;

           // Wait for the user to quit the program.
           Console.WriteLine("Press \'q\' to quit the sample.");
          // while (Console.Read() != 'q') ;
       }

       // Define the event handlers.
       private static void OnChanged(object source, FileSystemEventArgs e)
       {
           // Specify what is done when a file is changed, created, or deleted.
           Console.WriteLine("File: " + e.FullPath + " " + e.ChangeType);
       }

       private static void OnRenamed(object source, RenamedEventArgs e)
       {
           // Specify what is done when a file is renamed.
           Console.WriteLine("File: {0} renamed to {1}", e.OldFullPath, e.FullPath);
       }
   }
Posted
Updated 19-May-15 5:34am
v2
Comments
Herman<T>.Instance 19-May-15 11:13am    
Why don't you try what the FileWatcher does?
Sarita S 19-May-15 11:37am    
Running program on windows 8.
Andy Lanng 19-May-15 11:36am    
Why doesn't FileWatcher work? not NTFS?

Polling is the only answer, then. Set a timer (every 5 seconds?) and check the state of the folder verses the previous state. I'll write a new solution
Sarita S 20-May-15 1:20am    
I tried with timer.No effect

Try to use FileSystemWatcher:

C#
private void watch()
{
  FileSystemWatcher watcher = new FileSystemWatcher();
  watcher.Path = path;
  watcher.NotifyFilter = NotifyFilters.LastWrite;
  watcher.Filter = "*.*";
  watcher.Changed += new FileSystemEventHandler(OnChanged);
  watcher.EnableRaisingEvents = true;
}

private void OnChanged(object source, FileSystemEventArgs e)
{
  //you will be called as soon smth changes
}
 
Share this answer
 
v2
Comments
Andy Lanng 19-May-15 11:18am    
Dang - I was sure I got there first :Þ
You need the SystemFileWatcher:

https://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher%28v=vs.110%29.aspx[^]

Specifically SystemFileWatcher.Created event.

This will only work on NTFS, I think (citation needed)

It will not work on Linux mapped drives.

To watch Linux maps or Fat32 (etc...) you will have to poll the directory

Hope that helps ^_^
Andy
 
Share this answer
 
v3
If the SystemFileWatcher doesn't work then polling is the only viable option.

Try something like this:
C#
Timer _timer = new Timer(5000);
string _path = GetPathToWatch;

public FileWatcher(){
 _timer.Elapsed += TimerOnElapsed;
 _timer.Start()

}

string[] _fileList = null;

// The event will occur on a seperate thread
private void TimerOnElapsed(object sender, ElapsedEventArgs elapsedEventArgs)
{
    DirectoryInfo di = new DirectoryInfo(_path);
    
    //get a list of the files that are there already
    string[] newFileList = di.GetFiles()

    // if _fileList is null then this is the first run
    if(_fileList !=Null)
    {
        
        string[] newfiles = newFileList.Except(_fileList);
        foreach(string newFilePath in newfiles){
           //raise an event?
           //pass to a method?
        } 
    }
    //Update the existing files list 
    _fileList = newFileList; 
}
 
Share this answer
 

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