简体   繁体   English

使用默认应用在Windows应用商店应用中打开文件

[英]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: 我有以下两个功能可以在Windows应用商店项目中保存和读取文件:

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. 现在,我尝试获取文件并使用默认应用程序将其打开以打开这些文件,在这种情况下,我试图打开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) (来自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 ). 您正在Package.Current.InstalledLocation StorageFolder上调用GetFileAsync ,但是您将其传递到位于其他位置的StorageFile的路径( ApplicationData.Current.LocalFolder )。 If you want to get a file from LocalFolder , get a reference to it: 如果要从LocalFolder获取文件,请获取LocalFolder文件的引用:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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