Windows Dev Center

Handle error when save file to the storage


  • Hi,

    How can we know the description or type of error when we fail to save file on the storage? Do we have any documents about that? My application have to save a lot of images to cache. I need to check when cache is full, then delete the old data. But I don't want to count the size of storage to check because it maybe  slow. So I want to check the error if we fail to save file. And if error caused by not enough space to save, I will delete data. Can we do that?



    13 มีนาคม 2555 9:29


  • Hi Vivian,

    We also provide ways to determine the amount of available free space:

    System.FreeSpace can be retrieved with StorageItem.retrievePropertiesAsync and gives you the amount of free space for the volume where the file or folder is stored.

    We recommend that you set a maximum size for your cache and start clearing the cache whenever free space becomes low, or whenever you hit the maximum size. The fastest way to get the total size of your cache is:

    // A common file query by name will be "deep",
    // which means it will look at all subfolders
    var options = new Windows.Storage.Search.QueryOptions(Windows.Storage.Search.CommonFileQuery.orderByName, ["*"]);
    // This will get all files in your cache folder
    var fileQuery = cacheFolder.createFileQueryWithOptions(options);
    fileQuery.getFilesAsync().done(function(files) {
        var totalSize = 0;
        var promises = {
           return file.getBasicPropertiesAsync();
        // Getting file size is async, so we wait for all promises to complete
        WinJS.Promise.join(promises).then(function(properties) {
            for (var i in properties) {
                totalSize += properties[i].size;
    Hope this helps!
    • เสนอเป็นคำตอบโดย Marc Wautier - MSFTMicrosoft 28 มีนาคม 2555 0:19
    • ทำเครื่องหมายเป็นคำตอบโดย Vivian Ng 2 เมษายน 2555 3:36
    27 มีนาคม 2555 22:30