开发者

Flex 3 & Air 2: Automatically detect when files on a directory are updated

A crazy idea just dropped from the sky and hit me in the head xD. I was wondering if it is possible to make and App capable of listening when the user "adds" new files to a directory.

Example:

  1. The User opens up our Application.
  2. The user adds new files on the desktop (using the Microsoft Explorer).
  3. Our application automatically detects that new files have been added and executes a function or whatever.

Sound interesting right开发者_StackOverflow中文版?

Maybe, this could be done using a programming language like Visual Basic and open the executable with the NativeProcess api and listen for an stdOut event... (:

Anyone got and idea to share with us? :)

Thanks Lombardi


AIR can handle this natively...

the FileSystemList class fires an event directoryChange whenever a file in the watched directory changes.

You can even use it to watch for drives being mounted (I think Christian Cantrell showed that one off)


Ok, I think I'm getting closer, check out this solution! :)

private var CheckDelay:Timer = new Timer(5000, 0);

private function InitApp():void
{
   CheckDelay.addEventListener(TimerEvent.Timer, CheckForNewFiles, false, 0, true);
   CheckDelay.start();
}

private function CheckForNewFiles(event:TimerEvent):void
{
   var FS:FileStream = new FileStream();
   var Buffer:File = File.applicationStorageDirectory.resolvePath("FilesBuffer.cmd");
   FS.open(Buffer, FileMode.Write);
   FS.writeUTFBytes("cd " + File.desktopDirectory.nativePath + "\r\n" + 
                     "dir /on /b > " + File.applicationStorageDirectory.resolvePath("FileList.txt").nativePath);
   FS.close();


   var Process:NativeProcess = new NativeProcess();
   var NPI:NativeProcessStartupInfo = NativeProcessStartupInfo(); // What a large name! xD
   NPI.executable = Buffer;
   Process.start(NPI);
   Process..addEventListener(NativeProcessExitEvent.EXIT, ReadFileList, false, 0, true);
} 

private function ReadFileList(event:Event):void
{
   var FS:FileStream = new FileStream();
   var Buffer:File = File.applicationStorageDirectory.resolvePath("FilesBuffer.cmd");
   FS.open(Buffer, FileMode.Read);
   var FileData:String = FS.readUTFBytes(FS.bytesAvailable);
   FS.close();

   var FileArray:Array = FileData.split("\r\n");

   var TempArray:ArrayCollection = new ArrayColletion();
   var TempFile:File;
   for(var i:int = 0;i<FileArray.length;i++){
    TempFile = new File(FileArray[i]);
    TempArray.addItem(TempFile);
   } 
}

At the end we got an Array (TempArray) that we could use on a datagrid (for example) with colums like: "extension, File Name, FilePath, etc.."

The files are updated every 5 seconds.

And, why we use all that code instead of a simple "File.getDirectoryListing()"? Because we are updating our application every 5 seconds, if why use getDirectoryListing(), our application will take much more RAM and also, the cmd command is much faster... :)

If you have a better idea, please share it with us! Thank you! :D


1 excellent solution for Windows: use Visual Studio, build the .net app found here http://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher.aspx In Adobe AIR use the native process to listen for change events dispatched by .net

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜