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

时间:2014-08-05 15:11:15

标签: c# windows-store-apps microsoft-metro windows-8.1

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

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

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

1 个答案:

答案 0 :(得分:0)

试试这个:

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

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

此外,您可能只需创建一个库来管理各种文件操作。

相关问题