Quickstart: Configure removable storage as an AutoPlay device (HTML)
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.
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.
Prerequisites
You must have a volume device such as a thumb drive or memory card to complete the instructions in this quickstart.
Microsoft Visual Studio
Instructions
1. Create an autorun.inf file
In the root drive of your volume device, add a file named autorun.inf. Open the autorun.inf file and add the following text.
[AutoRun] CustomEvent=AutoPlayCustomEventQuickstart
2. Create a new project and add AutoPlay declarations
Open Visual Studio and select New Project from the File menu. In the Javascript section, select Windows Store. Name the app 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 Folders 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.
3. Add HTML UI
Open the Default.html file and place the following HTML in the <body> section.
<h2>Files</h2> <div id="files" style="width:400px;height:600px;font-size:large;" />
4. Add activation code
The code in this step calls a function 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 that are passed to the app. You can retrieve this folder from the first element of the detail.files property. You can tell that the app was launched as a handler for an AutoPlay content event if the detail.kind property of the activation event arguments returns a value of Windows.ApplicationModel.Activation.ActivationKind.file.
Open the js folder. Open the Default.js file and replace the default onactivated function with the following code.
var filesDiv; app.onactivated = function (args) { if (args.detail.kind === activation.ActivationKind.file) { filesDiv = document.getElementById("files"); displayFiles(args.detail.files[0]); } args.setPromise(WinJS.UI.processAll()); };
5. Add code to display folder names
In the Default.js file, add the following code after the onactivated function.
function displayFiles(folder) { var options = new Windows.Storage.Search.QueryOptions(); options.fileTypeFilter.append(".ms"); var query = folder.createFileQueryWithOptions(options); query.getFilesAsync().done( function (files) { files.forEach( function (f) { filesDiv.innerHTML += " " + f.name + "<br/>"; }); }); }
6. Build and run the app
- 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.
Summary and next steps
In this tutorial, you identified a volume device as an AutoPlay device by defining a custom AutoPlay event name in an autorun.inf file. You then created an app that registers as a handler for that custom event.