Protected Identity
Protected Identity

Reputation: 241

Opening files from Windows Explorer in my Metro App

Coming from a Windows Forms background, I am used to being able to handle arguments, in the Program.cs file, passed to my application, when a user tries to open a Text file from Windows Explorer, so that my application can display its contents to the user.

However, in Metro style Apps, we don't have the Program.cs file anymore. We have the App.xaml or App.xaml.cs file.

Seeing as though I cannot find relevant documentation on this, I could just try doing it "the usual" way, in the App.xaml.cs file but I'm not even sure if that's the right way to go about it. I have Added the appropriate Capabilities and File type Associations to my Metro style App, but other than that I am don't know where to start.

How can we open a supported file from the Documents folder into our own Metro style Apps?

Upvotes: 1

Views: 1623

Answers (3)

shedskin
shedskin

Reputation: 45

  1. open package.appxmanifest in Solution Explorer.
  2. Select the Declarations tab.
  3. Select File Type Associations from the drop-down list and click Add.
  4. Enter txt as the Name.
  5. Enter .txt as the File Type.
  6. Enter “images\Icon.png” as the Logo.

add the proper icons in the app package

and in c#, You need to handle OnFileActivated event

protected override void OnFileActivated(FileActivatedEventArgs args)
{
 // TODO: Handle file activation

// The number of files received is args.Files.Size
// The first file is args.Files[0].Name
}

Upvotes: 0

Dominic Hopton
Dominic Hopton

Reputation: 7292

You handle this by two specific steps:

  1. Declare the file extension in your manifest. You can do this by opening the package.appxmanifest from Solution Explorer in VS, Going to the Declarations Tab, and adding the "File Type Association" declaration & relevant information.
  2. In your activation handler, you will see the even has a "Kind" parameter. This will be "file" for a file launch (from explorer, or elsewhere). You will get the files in the "files" property on the same object.

Full details are here. Once you've got the files, you can use the standard Windows.Storage API's to access those files.

Upvotes: 0

Jeff Brand
Jeff Brand

Reputation: 5633

See How to handle file activation @ http://msdn.microsoft.com/en-us/library/windows/apps/xaml/hh779669.aspx

Upvotes: 1

Related Questions