2012-09-17 61 views
1

我有一個文件路徑(如「C:\ ...」)的陣列,我想用我的應用程序中的默認應用程序打開它們。讓我們說這是一個列表,當我點擊其中一個時,它會打開。在Windows 8 Metro App C#中打開文件

這是啓動的文件異步方式:

await Windows.System.Launcher.LaunchFileAsync(fileToLaunch); 

它需要一個Windows.Storage.StorageFile類型的文件,其中有一個Path只讀屬性,所以我不能設置路徑。我怎樣才能打開他們,一旦他們點擊/點擊?

+1

選中此項:http://stackoverflow.com/questions/9527644/launching-a-desktop-application-with-a-metro-style-app – SeToY

回答

4

從我的鏈接複製的評論:

// Path to the file in the app package to launch 
    string exeFile = @"C:\Program Files (x86)\Steam\steamapps\common\Skyrim\TESV.exe"; 

    var file = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync(exeFile); 

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

當然你可以省略var options = ** -Part所以ApplicationPicker沒有得到打開

,或者您可以使用此:

StorageFile fileToLaunch = StorageFile.GetFileFromPathAsync(myFilePath); 
await Windows.System.Launcher.LaunchFileAsync(fileToLaunch); 
0

的答案是這樣的樣品中:http://code.msdn.microsoft.com/windowsapps/Association-Launching-535d2cec

簡短的回答是:

// First, get the image file from the package's image directory. 
string fileToLaunch = @"images\Icon.Targetsize-256.png"; 
var file = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync(fileToLaunch); 

// Next, launch the file. 
bool success = await Windows.System.Launcher.LaunchFileAsync(file); 
0

最好的解決辦法是這樣的:

string filePath = @"file:///C:\Somewhere\something.pdf"; 

    if (filePath != null) 
    { 
     bool success = await Windows.System.Launcher.LaunchUriAsync(new Uri(filePath)); 
     if (success) 
     { 
      // File launched 
     } 
     else 
     { 
      // File launch failed 
     } 
    } 

Tha app l使用系統默認應用程序啓動它,在這種情況下使用Adobe Reader。

相關問題