如何在AIR中的目录下检测何时更改文件?

时间:2011-09-22 20:18:15

标签: flash actionscript-3 file air directory

我见过使用计时器的示例,获取文件列表,并检查每个文件是否有变化,但有更简单的方法可以做到这一点,例如......

<mx:FileSystemList id="fs" visible="false" />

private function onCreationComplete():void
{
    fs.directory = File.applicationDirectory.resolvePath('../../assets');
    if (fs.directory.exists)
      addEventListener(FileEvent.DIRECTORY_CHANGE, onDirectoryChange);
}

private function onDirectoryChange(e:FileEvent):void
{
    trace("file was changed");
}

文件更改

时似乎没有触发

1 个答案:

答案 0 :(得分:0)

使用FileMonitor类。

http://www.mikechambers.com/blog/2009/03/11/monitoring-file-changes-in-adobe-air/

教程中的代码:

import com.adobe.air.filesystem.FileMonitor;
import flash.filesystem.File;
import flash.events.Event;
import com.adobe.air.filesystem.events.FileMonitorEvent;

private var monitor:FileMonitor;

private function onSelectButtonClick():void
{
    var f:File = File.desktopDirectory;
    f.addEventListener(Event.SELECT, onFileSelect);
    f.browseForOpen("Select a File to Watch.");
}

private function onFileSelect(e:Event):void
{
    var file:File = File(e.target);

    if(!monitor)
    {
        monitor = new FileMonitor();
        monitor.addEventListener(FileMonitorEvent.CHANGE, onFileChange);
        monitor.addEventListener(FileMonitorEvent.MOVE, onFileMove);
        monitor.addEventListener(FileMonitorEvent.CREATE, onFileCreate);
    }

    monitor.file = file;
    monitor.watch();
}

private function onFileChange(e:FileMonitorEvent):void
{
    trace("file was changed");
}

private function onFileMove(e:FileMonitorEvent):void
{
    trace("file was moved");
}

private function onFileCreate(e:FileMonitorEvent):void
{
    trace("file was created");
}