locked
Opening a file in windows store app, with default app

    Question

  • 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?


    • Edited by Thought2 Monday, February 17, 2014 1:03 PM
    Monday, February 17, 2014 12:55 PM

Answers

  • StorageFolder local = Windows.Storage.ApplicationData.Current.LocalFolder; StorageFile file = await local.GetFileAsync(path);

    instead of

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

    • Marked as answer by Thought2 Monday, February 17, 2014 2:34 PM
    Monday, February 17, 2014 2:34 PM