如何从另一个方法访问StorageFile

时间:2015-09-22 23:01:07

标签: c# windows windows-runtime microsoft-metro

所以我正在制作一个Windows商店应用程序,您通过文件选择器选择一个按钮的文件,然后使用另一个按钮处理该文件,但我无法将所选文件转换为处理方法。
由于Picker将我的一个文本块设置为要为用户显示的文件路径,我尝试使用:
StorageFile file = await StorageFile.GetFileFromPathAsync(fullFilePath.Text);
但由于Windows RT的限制,我只是从大多数地方获取访问权限 关于尝试什么的任何其他建议?

点击第一个按钮:

private async Task getFile()
        {
            FileOpenPicker openPicker = new FileOpenPicker();
            openPicker.ViewMode = PickerViewMode.List;
            openPicker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary;
            openPicker.FileTypeFilter.Add(".txt");

            StorageFile file = await openPicker.PickSingleFileAsync();
            if (file != null)
            {
                fullFilePath.Text = file.Path;
            }
            else
            {
                updateStatus("File Selection cancelled.");
            }
        }

第二个按钮启动,但需要使用上面的文件

private async Task processFile()
{
    ...
    string content = await FileIO.ReadTextAsync(file);
    ...
}

1 个答案:

答案 0 :(得分:1)

StorageFile成为班级中的一个字段:

class MyClass
{
  StorageFile m_pickedFile;

  async Task GetFile()
  {
    // Setup the picker...
    m_pickedFile = await openPicker.PickSingleFileAsync();

    // Show the path to the user...
  }

  async Task ProcessFile()
  {
    if (m_pickedFile != null)
    {
      // now use m_pickedFile...
    }
  }
}
相关问题