Lgn
Lgn

Reputation: 10099

Open a file in Windows 8 Metro App C#

I have an array with file paths (like "C:\...") and I would like to open them with the default app, from my app. Let's say it's a list and when I click one of them, it opens.

This is the way to launch a file async:

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

It requires a Windows.Storage.StorageFile type of file, which has a Path read-only property, so I cannot set the Path. How can I open them once they're tapped/clicked?

Upvotes: 1

Views: 8228

Answers (4)

Inkogo
Inkogo

Reputation: 1

Best solution is this:

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 launches it with the system default application, in this case with the Adobe Reader.

Upvotes: 0

user1998494
user1998494

Reputation: 634

the answer is within this sample: http://code.msdn.microsoft.com/windowsapps/Association-Launching-535d2cec

short answer is :

// 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);

Upvotes: 0

SeToY
SeToY

Reputation: 5895

Copied from my link in the comments:

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

You can of course omit the var options = **-Part so the ApplicationPicker doesn't get opened

or you can use this:

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

Upvotes: 4

Parv Sharma
Parv Sharma

Reputation: 12705

you should use this method http://msdn.microsoft.com/en-US/library/windows/apps/windows.storage.storagefile.getfilefrompathasync
on the Type StorageFile

This method is used to get file if you have a path already

Upvotes: 2

Related Questions