简体   繁体   中英

Opening a file in windows store app, with default app

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM