2015-09-22 38 views
0

因此,即時製作一個Windows應用商店應用程序,您可以通過文件選擇器選擇一個文件,然後用另一個按鈕處理該文件,但即時通過選擇文件獲取處理方法時遇到問題。
由於選擇器設置所要顯示我的文字塊到文件的路徑之一我已經嘗試使用用戶:
StorageFile file = await StorageFile.GetFileFromPathAsync(fullFilePath.Text);
但由於到Windows RT限制我只可以訪問它從大多數位置
否認 有關嘗試的其他建議?如何從另一種方法訪問StorageFile

第一個按鈕點擊:

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

可能[從另一個方法調用文件]的副本(http://stackoverflow.com/questions/16797452/call-file-from-another-meth od) –

+0

@chue啊,謝謝那個病人已經通讀了 – Toxicable

+0

備註:作爲一般規則,您應該避免在Universal Windows應用程序中使用文件路徑進行編程 - 主要(僅限於?)使用它們是傳遞給Win32採用文件路徑的功能,或可能向用戶顯示的功能。你永遠不應該在任何地方保存文件路徑,並期望它再次工作(*即使是你自己的數據*的路徑)。 –

回答

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