【问题标题】:What is the correct way to use StorageFile.GetFileFromPathAsync(pth) in Windows 8在 Windows 8 中使用 StorageFile.GetFileFromPathAsync(pth) 的正确方法是什么
【发布时间】:2013-01-25 10:43:23
【问题描述】:

我有一个方法可以返回文件的正确路径

    StorageFile newFile = null;
        try
        {
            string pth = Path.Combine(ApplicationData.Current.LocalFolder.Path, dbPath);
            newFile = StorageFile.GetFileFromPathAsync(pth).GetResults(); ;
        }
        catch (FileNotFoundException) { newFile = null; }
        if (newFile == null)
        {
            string pth = Package.Current.InstalledLocation.Path + @"\Scriptures\" + dbPath;
            StorageFile oldFile = StorageFile.GetFileFromPathAsync(pth).GetResults();
            newFile = oldFile.CopyAsync(ApplicationData.Current.LocalFolder, dbPath, NameCollisionOption.FailIfExists).GetResults();
        }
        if (newFile == null) { throw new FileNotFoundException("Database File not Found"); }
        return newFile.Path;

但有时我会遇到错误

    StorageFile.GetFileFromPathAsync(pth).GetResults()

抛出的错误是:

   StorageFile newFile = null;
        try
        {
            string pth = Path.Combine(ApplicationData.Current.LocalFolder.Path, dbPath);
            newFile = StorageFile.GetFileFromPathAsync(pth).GetResults(); ;
        }
        catch (FileNotFoundException) { newFile = null; }
        if (newFile == null)
        {
            string pth = Package.Current.InstalledLocation.Path + @"\Scriptures\" + dbPath;
            StorageFile oldFile = StorageFile.GetFileFromPathAsync(pth).GetResults();
            newFile = oldFile.CopyAsync(ApplicationData.Current.LocalFolder, dbPath, NameCollisionOption.FailIfExists).GetResults();
        }
        if (newFile == null) { throw new FileNotFoundException("Database File not Found"); }
        return newFile.Path;

我做错了什么?我是 c# 和线程的新手,这个错误让我很生气,因为有时它有效,有时它不。 请帮忙!

【问题讨论】:

  • 尝试 newFile = await StorageFile.GetFileFromPathAsync(pth);

标签: c#-4.0 windows-8 windows-runtime storagefile


【解决方案1】:

运行时应用没有权限!你不能用这个方法访问文件。 检查这个page

【讨论】:

  • 虽然这可能会回答问题,it would be preferable 在此处包含更多答案的基本部分,并提供链接以供参考。此外,说“这个 API 在提供它的唯一环境中根本不起作用”是一个相当奇特的断言。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-19
  • 2020-01-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多