繁体   English   中英

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

[英]Opening a file in windows store app, with default app

我有以下两个功能可以在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;
    }

现在,我尝试获取文件并使用默认应用程序将其打开以打开这些文件,在这种情况下,我试图打开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
        }
    }

但是当我将文件名传递给函数时,它不会被重新识别

附加信息:文件名,目录名称或卷标签语法不正确。 (来自HRESULT的异常:0x8007007B)

有人可以帮忙吗?

这是错误的来源:

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

您正在Package.Current.InstalledLocation StorageFolder上调用GetFileAsync ,但是您将其传递到位于其他位置的StorageFile的路径( ApplicationData.Current.LocalFolder )。 如果要从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