将文件从应用程序安装文件夹复制到本地存储

本文关键字:存储 复制 文件夹 文件 应用程序 安装 | 更新日期: 2023-09-27 18:30:23

我正在尝试将文件从我的 Windows 8 应用程序的安装位置复制到它的本地存储。我一直在四处研究并试图做到这一点无济于事。这就是我到目前为止想出的,但我不确定我哪里出错了。

    private async void TransferToStorage()
    {

        try
        {
            // Get file from appx install folder
            Windows.ApplicationModel.Package package = Windows.ApplicationModel.Package.Current;
            Windows.Storage.StorageFolder installedLocation = package.InstalledLocation;
            StorageFile temp1 = await installedLocation.GetFileAsync("track.xml");
            // Read the file
            var lines = await FileIO.ReadLinesAsync(temp1);
            //Create the file in local storage
            StorageFile myStorageFile = await localFolder.CreateFileAsync("track_iso.xml", CreationCollisionOption.ReplaceExisting);
            // Write to it
            await FileIO.WriteLinesAsync(myStorageFile, lines);
        }
        catch (Exception)
        {
        }
    }

有什么想法吗?

将文件从应用程序安装文件夹复制到本地存储

自己解决了。这是遇到此问题/问题的其他任何人的方法:

   private async void TransferToStorage()
    {
        // Has the file been copied already?
        try
        {
            await ApplicationData.Current.LocalFolder.GetFileAsync("localfile.xml");
            // No exception means it exists
            return;
        }
        catch (System.IO.FileNotFoundException)
        {
            // The file obviously doesn't exist
        }
        // Cant await inside catch, but this works anyway
        StorageFile stopfile = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///installfile.xml"));
        await stopfile.CopyAsync(ApplicationData.Current.LocalFolder);
    }

没有理由读取所有行并将其写入另一个文件。只需使用File.Copy。