Quickstart: Configure removable storage as an AutoPlay device (XAML)
You can identify a volume device such as a memory card or thumb drive as an AutoPlay device when the volume device is connected to a PC. This is especially useful when you want to associate a specific app for AutoPlay to present to the user for your volume device.
Roadmap: How does this topic relate to others? See:
- Roadmap for Windows Runtime apps using C# or Visual Basic
- Roadmap for Windows Runtime apps using C++
Here we show how to identify your volume device as an AutoPlay device.
To identify your volume device as an AutoPlay device, add an autorun.inf file to the root drive of your device. In the autorun.inf file, add a CustomEvent key to the AutoRun section. When your volume device connects to a PC, AutoPlay will find the autorun.inf file and treat your volume as a device. AutoPlay will create an AutoPlay event by using the name that you supplied for the CustomEvent key. You can then create an app and register the app as a handler for that AutoPlay event. When the device is connected to the PC, AutoPlay will show your app as a handler for your volume device. For more info on autorun.inf files, see autorun.inf entries.
Device manufacturers:: AutoPlay doesn't automatically download the app that you create as a handler for your device. But as always, users can get your app from the Windows Store. To create a more streamlined experience, use device metadata to associate your app with your device. If you associate your app as the auto-installed app for the experience ID of your device, the operating system will discover the association when your device is connected to a PC. If the PC doesn't have your app installed, the operating system will automatically download and install your app. AutoPlay will present your app as the first option for the user to choose as the handler for your device. For more info, see AutoPlay for Windows Store device apps.
Objective: Create an app to handle a custom AutoPlay event for a volume device.
You must have a volume device such as a thumb drive or memory card to complete the instructions in this quickstart.
Microsoft Visual Studio
- In the root drive of your volume device, add a file named autorun.inf. Open the autorun.inf file and add the following text.
- Open Visual Studio and select New Project from the File menu. In the Visual C# or Visual Basic section, select Windows Store. Name the application AutoPlayCustomEvent and click OK.
- Open the Package.appxmanifest file and select the Capabilities tab. Select the Removable Storage capability. This gives the app access to the files and folders on removable storage devices.
- In the manifest file, select the Declarations tab. In the Available Declarations drop-down list, select AutoPlay Content and click Add. Select the new AutoPlay Content item that was added to the Supported Declarations list.
Note Alternatively, you can also choose to add an AutoPlay Device declaration for your custom AutoPlay event.
In the Launch Actions section for your AutoPlay Content event declaration, enter the following values for the first launch action.
Setting Value Verb show Action Display Name Show Files Content Event AutoPlayCustomEventQuickstart
The Content Event value is the text that you supplied for the CustomEvent key in your autorun.inf file. The Action Display Name setting identifies the string that AutoPlay displays for your app. The Verb setting identifies a value that is passed to your app for the selected option. You can specify multiple launch actions for an AutoPlay event and use the Verb setting to determine which option a user has selected for your app. You can tell which option the user selected by checking the verb property of the startup event arguments passed to your app. You can use any value for the Verb setting except, open, which is reserved.
- In the Available Declarations drop-down list, select File Type Associations and click Add. In the Properties of the new File Type Associations declaration, set the Display Name field to Show .ms Files and the Name field to ms_association. In the Supported File Types section, click Add New. Set the File Type field to .ms. For content events, AutoPlay filters out any file types that aren't explicitly associated with your app.
- Save and close the manifest file.
- Open the MainPage.xaml file and add the following XAML to the default <Grid> section.
The code in this step calls a method to display the folders in the root drive of your volume device. For the AutoPlay content events, AutoPlay passes the root folder of the storage device in the startup arguments passed to the application during the OnFileActivated event. You can retrieve this folder from the first element of the Files property.
- Open the App.xaml.cs or App.xaml.vb file and add the following code to the App class.
Protected Overrides Sub OnFileActivated(args As FileActivatedEventArgs) Dim rootFrame = TryCast(Window.Current.Content, Frame) Dim page = TryCast(rootFrame.Content, MainPage) ' Call ShowFolders with root folder from device storage. page.DisplayFiles(TryCast(args.Files(0), Windows.Storage.StorageFolder)) MyBase.OnFileActivated(args) End Sub
- In the MainPage.xaml.cs or MainPage.xaml.vb file add the following code to the MainPage class.
Friend Async Sub DisplayFiles(folder As Windows.Storage.StorageFolder) For Each f In Await ReadFiles(folder, ".ms") FilesBlock.Text &= " " & f.Name & vbCr Next End Sub Friend Async Function ReadFiles(folder As Windows.Storage.StorageFolder, fileExtension As String) As Task(Of IReadOnlyList(Of Windows.Storage.StorageFile)) Dim options = New Windows.Storage.Search.QueryOptions() options.FileTypeFilter.Add(fileExtension) Dim query = folder.CreateFileQueryWithOptions(options) Dim files = Await query.GetFilesAsync() Return files End Function
- Press F5 to build and deploy the app (in debug mode).
- To run your app, insert a memory card or another storage device into your PC. Then select your app from the list of AutoPlay handler options.
In this tutorial, you identified a volume device as an AutoPlay device by defining a custom AutoPlay event name using an autorun.inf file. You then created an app that registers as a handler for that custom event.