Toxicable
Toxicable

Reputation: 1709

How to access StorageFile from another method

So im making a windows store app that you select a file with one button, via file picker, then with another button it processes that file but im having trouble getting the selected file to processing method.
Since the Picker sets one of my text blocks to the path of the file to be displayed for the user i've tried using:
StorageFile file = await StorageFile.GetFileFromPathAsync(fullFilePath.Text);
But due to Windows RT limitations I just get access it denied from most locations
Any other suggestions on what to try?

First button click:

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

Second button start this but needs to use the file from above

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

Upvotes: 0

Views: 132

Answers (1)

Peter Torr
Peter Torr

Reputation: 12019

Make the StorageFile a field in your class:

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...
    }
  }
}

Upvotes: 1

Related Questions