从win8 metro应用程序的文件夹中加载pdf文件

本文关键字:加载 pdf 文件 文件夹 win8 metro 应用程序 | 更新日期: 2023-09-27 18:16:43

我正在尝试从我的win8应用程序中的文件夹加载pdf文件。它在测试文件夹中。I try

StorageFile file = StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Test/example.pdf"));

这给了我一个未找到的文件。但是,如果我将example.pdf的扩展名更改为jpg,然后更改代码以查找example.jpg,则它确实可以正常工作。发生了什么事?

从win8 metro应用程序的文件夹中加载pdf文件

试试这个:

public async Task<StorageFile> GetFileAsync(string directory, string fileName)
{
    string filePath = BuildPath(directory, fileName);
    string directoryName = Path.GetDirectoryName(filePath);
    StorageFolder storageFolder = await StorageFolder.GetFolderFromPathAsync(directoryName);
    StorageFile storageFile = await storageFolder.GetFileAsync(fileName);
    return storageFile;
}
public string BuildPath(string directoryPath, string fileName)
{
    string directory = Path.Combine(Package.Current.InstalledLocation.Path, directoryPath);
    return Path.Combine(directory, fileName);
}

从您的客户端,传入文件的目录(相对于项目)路径,然后为第二个参数提供文件名。

另外,最好创建一个库来管理各种文件操作。