0

我試圖將應用程序包文件夾複製到獨立存儲。這是我一起來的。我得到一個JavaScript錯誤不能創建已經存在的文件。我有困難遍歷子文件夾。將應用程序包文件夾複製到Windows Store應用程序的獨立存儲

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); 
          }); 
         }); 
        }); 
       }); 
      }); 
     }); 
    } 
} 
+0

我很好奇 - 爲什麼你要複製整個包文件夾,其中包括所有的代碼等?似乎你主要只想複製數據子文件夾,這樣你可以使用包內容作爲基礎,然後在運行時更新數據。誠然,在JS應用程序中,代碼通常並不那麼大;仍然,這裏的方法意味着你要複製啓動畫面圖像,庫等,這隻會浪費存儲空間。 – 2014-12-04 18:46:21

回答

0

我混合filelist和文件夾列表一起導致應用程序崩潰。爲每個過程創建功能是解決方案。

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); 

       }); 
      }); 
     } 
    }); 
} 
相關問題