6 Replies Latest reply on Oct 27, 2010 10:40 PM by ariv.l

    hwo to delete files resides in the File.applicationStorageDirectory?

    ariv.l

      i created a method saveLocally() to store the images temporarily which are obtained from the server.

       

            saveLocally(fileName:String, data:ByteArray):void
              {
                  var file:File = File.applicationStorageDirectory.resolvePath(fileName);
                  var fileStream:FileStream = new FileStream();
                  fileStream.open(file, FileMode.WRITE);
                  fileStream.writeBytes(data, 0, data.length);
                  fileStream.close();    
              }

                                   these files are stored in "… C:\Documents and Settings\Administrator\Application Data\ImageGallery\Local Store"

       

      after i closed my application the files which are stored in the Local Store Directory are still avalilable. But want to delete those images when i clicked the close button.

       

       

      Thanks in advance...

       

       

       

        • 1. Re: hwo to delete files resides in the File.applicationStorageDirectory?
          BhaskerChari Level 4

          Hi Ariv,

           

          Try to call the function below when you click the close button in your application..

           

           

          public function deleteImages()
          {
             var myFile:File = File.applicationStorageDirectory.resolvePath("ImagesFolderName");
            
             if(myFile.exists)
             {
              myFile.deleteDirectory(true);   
             }
          }

           

          In the above ImagesFolderName is the name of your folder in which your Images are actually saved.

           

           

          Thanks,

          Bhasker Chari

          • 2. Re: hwo to delete files resides in the File.applicationStorageDirectory?
            injpix Level 3

            In an addition to Bhasker Chari's comment, I believe you cannot delete a file or directory when its being read or written to.  So you may need to listen for an event before invoking the delete method.

            • 3. Re: hwo to delete files resides in the File.applicationStorageDirectory?
              BhaskerChari Level 4

              @myIP,

               

              Yes you are right in that case he need to delete each file asynchronously by registering an eventListner.

               

              Thanks,

              Bhasker

              • 4. Re: hwo to delete files resides in the File.applicationStorageDirectory?
                ariv.l Level 1

                Hi Thanks for your response,

                 

                In my WindowedApplication's close event ----- close="onclose()"

                in my scirpt i tried,

                 

                public function onclose():void

                {

                      File.applicationStorageDirectory.resolvePath("Local Store");

                 

                               // * as well as     

                               // * File.applicationStorageDirectory.resolvePath("ImageGallery");


                       if(myFile.exists)
                       {
                            myFile.deleteDirectory(true);  
                       }

                }

                 

                both are not working,

                 

                my temperorary image location is C:\Documents and Settings\Administrator\Application Data\ImageGallery\Local Store\image1.png

                 

                now what should i give in the  File.applicationStorageDirectory.resolvePath("");

                • 5. Re: hwo to delete files resides in the File.applicationStorageDirectory?
                  BhaskerChari Level 4

                  Hi Ariv,

                   

                  Try to save the images in a Folder instead of directly saving in the Local Store directory in the  "C:\Documents and Settings\Administrator\Application Data\ImageGallery\Local Store" path.

                   

                  If you save them inside a particular folder then you can directly point to that folder where images reside and you can easily delete the contents with out any problem for other contents in the Local Store directory.

                   

                  If you directly save the images in your Local Store directory without any sub directory then it will be difficult for you to delete all the images at once as you may have other contents in the Local Store along with your images and so you cannot delete them at once and you need to point to each image file and delete single image file.

                   

                  So in order to avoid this you can store all the images in seperate folder in your ApplicationStorage directory and delete the contents(images) of the folder directly.

                   

                   

                  So create a folder Images in the  Local Store directory and point to that Images drectoy in your code "C:\Documents and Settings\Administrator\Application Data\ImageGallery\Local Store\Images\image1.png"

                   

                  Change your code as below:

                   

                  Note: Here File.applicationStorageDirectory itself points to the path -->  "C:\Documents and Settings\Administrator\Application Data\ImageGallery\Local Store"

                   

                   

                  No need to of using File.applicationStorageDirectory.resolvePath("Local Store"); or  File.applicationStorageDirectory.resolvePath("ImageGallery");

                   

                  Try to use debugging as what path you are actually getting then you know whats exactly happening...

                   

                   

                  public function onclose():void

                  {

                       //Here Images folder is the new folder now in which your images should be saved.

                        var myFile:File = File.applicationStorageDirectory.resolvePath("Images");


                         if(myFile.exists)
                         {
                              myFile.deleteDirectory(true);  
                         }

                  }

                  Thanks,

                  Bhasker

                  • 6. Re: hwo to delete files resides in the File.applicationStorageDirectory?
                    ariv.l Level 1

                    Hi Bhasker,

                     

                         Ya you are correct, i should create a seprate folder to store my images so that it looks perfect. Now i got my result...

                    Thanks for your response, every time when i got problem you cleared it.