如何查看目录,进行更新?观察添加或删除的内容? [英] How to watch directory,for updation of it? Watch to anything added or deleted?

查看:83
本文介绍了如何查看目录,进行更新?观察添加或删除的内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想像Windows一样开发照片屏幕保护程序。与Windows照片屏幕保护程序的问题是,如果有任何东西被添加到照片文件夹。它没有得到更新

,直到我们重新启动它。我想开发应用程序将收到有关更改的通知而不重新启动它。如何实现这一点??

尝试使用文件观察程序选项无效。

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.

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);
       }
   }

推荐答案

尝试使用FileSystemWatcher:



Try to use FileSystemWatcher:

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
}


你需要SystemFileWatcher:



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



特别是SystemFileWatcher.Created事件。



这只适用于NTFS,我认为(引用需要)



它不适用于Linux映射驱动器。



要观看Linux地图或Fat32(等等) ...)你将不得不轮询目录



希望有所帮助^ _ ^

Andy
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


如果SystemFileWatcher不起作用,那么轮询是唯一可行的选择。



尝试这样的事情:

If the SystemFileWatcher doesn't work then polling is the only viable option.

Try something like this:
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; 
}


这篇关于如何查看目录,进行更新?观察添加或删除的内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆