2014-02-17 67 views
0

我有這兩個函數來保存和讀取文件在Windows商店應用項目:打開在Windows應用商店中的應用程序文件,使用默認的應用程序

public async Task GravaFicheiro(string url,string nome) 
    { 
     HttpClient client = new HttpClient(); 

     HttpResponseMessage message = await client.GetAsync(url); 

     StorageFolder myfolder = Windows.Storage.ApplicationData.Current.LocalFolder; 
     StorageFile sampleFile = await myfolder.CreateFileAsync(nome, CreationCollisionOption.ReplaceExisting); 
     byte[] file = await message.Content.ReadAsByteArrayAsync(); 

     await FileIO.WriteBytesAsync(sampleFile, file); 
     var files = await myfolder.GetFilesAsync(); 

    } 

    public async Task<StorageFile> LeFicheiro(string nome) 
    { 

     StorageFolder myfolder = Windows.Storage.ApplicationData.Current.LocalFolder; 
     StorageFile sampleFile = await myfolder.CreateFileAsync(nome, CreationCollisionOption.OpenIfExists); 

     return sampleFile; 
    } 

現在我試着去得到一個文件,並打開它與默認應用程序打開這些文件,在這種特殊情況下,即時通訊設法打開PDF文件。

public async void DefaultLaunch(string path) 
    { 
     // Path to the file in the app package to launch 

     string p2 = Windows.Storage.ApplicationData.Current.LocalFolder.Path +"\\"+ path; 
     var file = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync(p2); 

     if (file != null) 
     { 
      // Set the option to show the picker 
      var options = new Windows.System.LauncherOptions(); 
      options.DisplayApplicationPicker = true; 

      // Launch the retrieved file 
      bool success = await Windows.System.Launcher.LaunchFileAsync(file, options); 
      if (success) 
      { 
       // File launched 
      } 
      else 
      { 
       // File launch failed 
      } 
     } 
     else 
     { 
      // Could not find file 
     } 
    } 

,但是當我通過文件名的功能,它不得到recoginzed

Additional information: The filename, directory name, or volume label syntax is incorrect. (Exception from HRESULT: 0x8007007B)

誰能幫助?

回答

1

這是錯誤的來源:

string p2 = Windows.Storage.ApplicationData.Current.LocalFolder.Path +"\\"+ path; 
var file = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync(p2); 

您在Package.Current.InstalledLocationStorageFolder調用GetFileAsync,但你傳遞一個路徑StorageFile在不同的位置(ApplicationData.Current.LocalFolder)。 如果你想從LocalFolder獲取文件,得到它的引用:

StorageFolder local = Windows.Storage.ApplicationData.Current.LocalFolder; 
StorageFile file = await local.GetFileAsync(path); 
+0

我明白了,謝謝:) – Ric

相關問題