2016-06-21 42 views
0

我正在嘗試從我的應用程序文件夾中的文件,但我面對這樣的例外:錯誤HRESULT E_FAIL已經從COM組件的調用返回 - 的Windows 8.1

錯誤HRESULT E_FAIL已從調用COM組件返回。

System.Runtime.InteropServices.COMException

我試圖把文件移出資產的文件夾,並在根文件夾中找到它,但我收到了同樣的問題!

這裏是我的代碼:

try 
{ 
    StorageFile data = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx://Assets/data.json")); 
    //... 
} 
catch (FileNotFoundException exception) 
{ 
    Debug.WriteLine(exception); 
} 

任何幫助嗎?

回答

0

的問題是在你的文件URI,你必須把在URI ms-appx:///這一部分的另一個斜槓「/」,您的代碼將是這樣的:

try 
      { 
       StorageFile data = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Assets/data.json")); 
       //... 
      } 
      catch (FileNotFoundException exception) 
      { 
       Debug.WriteLine(exception); 
      } 

而且,請確保您設置Content在文件屬性菜單的Build Action中,否則您將面臨FileNotFoundException :)。

+0

那麼簡單!它現在正在工作。 –

相關問題