Copy file from application installation folder to local storage

I am trying to copy a file from the installed location of my Windows 8 app to its local storage. I have been doing research and trying to do this to no avail. This is what I have come up with so far, but I am not sure where I am going wrong.

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

        }

    }

      

Any ideas?

+3


source to share


2 answers


I decided myself. Here is the method for anyone else who comes across this question / problem:



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

      

+6


source


There is no reason to read all lines and write them to another file. Just use File.Copy .



+2


source







All Articles