Opening a file in windows store app, with default app

0

I have these 2 functions to save and read files on a windows store app project:

public async Task GravaFicheiro(string url,string nome)
    {
        HttpClient client = new HttpClient();

        HttpResponseMessage message = await client.GetAsync(url);

        StorageFolder myfolder = Windows.Storage.ApplicationData.Current.LocalFolder;
        StorageFile sampleFile = await myfolder.CreateFileAsync(nome, CreationCollisionOption.ReplaceExisting);
        byte[] file = await message.Content.ReadAsByteArrayAsync();

        await FileIO.WriteBytesAsync(sampleFile, file);
        var files = await myfolder.GetFilesAsync();

    }

    public async Task<StorageFile> LeFicheiro(string nome)
    {

        StorageFolder myfolder = Windows.Storage.ApplicationData.Current.LocalFolder;
        StorageFile sampleFile = await myfolder.CreateFileAsync(nome, CreationCollisionOption.OpenIfExists);

        return sampleFile;
    }

now im trying to get a file and opening it with the default app to open those files, in this particular case im trying to open a pdf.

public async void DefaultLaunch(string path)
    {
        // Path to the file in the app package to launch

        string p2 = Windows.Storage.ApplicationData.Current.LocalFolder.Path +"\\"+ path;
        var file = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync(p2);

        if (file != null)
        {
            // Set the option to show the picker
            var options = new Windows.System.LauncherOptions();
            options.DisplayApplicationPicker = true;

            // Launch the retrieved file
            bool success = await Windows.System.Launcher.LaunchFileAsync(file, options);
            if (success)
            {
                // File launched
            }
            else
            {
                // File launch failed
            }
        }
        else
        {
            // Could not find file
        }
    }

but when i pass the file name to the function, it doesnt get recoginzed

Additional information: The filename, directory name, or volume label syntax is incorrect. (Exception from HRESULT: 0x8007007B)

can anyone help?

c#
windows-8
windows-store-apps
asked on Stack Overflow Feb 17, 2014 by Ric • edited Feb 17, 2014 by Ric

1 Answer

1

This is the source of the error:

string p2 = Windows.Storage.ApplicationData.Current.LocalFolder.Path +"\\"+ path;
var file = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync(p2);

You are calling GetFileAsync on a Package.Current.InstalledLocation StorageFolder, but you are passing it a path to the StorageFile in different location (ApplicationData.Current.LocalFolder). If you want to get a file from LocalFolder, get a reference to it:

StorageFolder local = Windows.Storage.ApplicationData.Current.LocalFolder;
StorageFile file = await local.GetFileAsync(path);
answered on Stack Overflow Feb 17, 2014 by w.b • edited Feb 17, 2014 by w.b

User contributions licensed under CC BY-SA 3.0