Copying App Package Folder To Isolated Storage For Windows Store App

109 views Asked by At

I am trying to copy the app package folder to Isolated Storage. This what I've come with. Im getting a javascript error cannot create file which already exists. Im having trouble traversing through the subfolders.

    var root = Windows.ApplicationModel.Package.current.installedLocation;
    copySubDirectories(root);
    function copySubDirectories(subFolder) {
        currentFolder.createFolderAsync(subFolder.name).done(function (newFolder1) {
            subFolder.getFilesAsync().done(function (fileList1) {
                if (fileList1==null) {
                    subFolder.getFoldersAsync().done(function (folderList1) {
                        folderList1.forEach(function (subFolder2) {
                            currentFolder = newFolder1;
                            copySubDirectories(subFolder2);
                        });
                    });
                    return;
                }

                fileList1.forEach(function (subFile1) {
                    subFile1.copyAsync(newFolder1, subFile1.name, Windows.Storage.NameCollisionOption.replaceExisting).done(function () {
                        subFolder.getFoldersAsync().done(function (folderList1) {
                            folderList1.forEach(function (subFolder2) {
                                currentFolder = newFolder1;
                                copySubDirectories(subFolder2);
                            });
                        });
                    });
                });
            });
        });
    }
}
1

There are 1 answers

0
Query21 On BEST ANSWER

I mixed filelist and folderlist together causing the app to crash. Creating functions for each process was the solution.

var sourceFolder = Windows.ApplicationModel.Package.current.installedLocation;
var destinationFolder = Windows.Storage.ApplicationData.current.localFolder;



copyDirectories(sourceFolder,destinationFolder);


function copyFiles(source, destination) {
    source.getFilesAsync().done(function (fileList) {
        if (fileList.size >= 1) {
            fileList.forEach(function (subFile) {
                subFile.copyAsync(destination, subFile.name, Windows.Storage.NameCollisionOption.replaceExisting).done(function () { });
            });
        }
    });
}

function copyDirectories(source,destination){
    source.getFoldersAsync().done(function (folderList) {
        if(folderList.size>1){

            folderList.forEach(function (subFolder) {
                destination.createFolderAsync(subFolder.name).done(function (newFolder) {         


                    copyFiles(subFolder, newFolder);
                        copyDirectories(subFolder,newFolder);

                });
            });
        }
    });
}