Dela via


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:

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

  1. 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.

  2. 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.

  3. 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.

     

  4. 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.
  1. 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.

  2. Save and close the manifest file.

3. Add XAML UI

  • Open the MainPage.xaml file and add the following XAML to the default <Grid> section.

    <StackPanel Orientation="Vertical">
        <TextBlock FontSize="28" Margin="10,0,800,0">Files</TextBlock>
        <TextBlock x:Name="FilesBlock" FontSize="22" Height="600" Margin="10,0,800,0" />
    </StackPanel>
    

4. Add activation code

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 override void OnFileActivated(FileActivatedEventArgs args)
    {
        var rootFrame = Window.Current.Content as Frame;
        var page = rootFrame.Content as MainPage;
    
        // Call ShowFolders with root folder from device storage.
        page.DisplayFiles(args.Files[0] as Windows.Storage.StorageFolder);
    
        base.OnFileActivated(args);
    }
    
    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
    

5. Add code to display folder names

  • In the MainPage.xaml.cs or MainPage.xaml.vb file add the following code to the MainPage class.

    internal async void DisplayFiles(Windows.Storage.StorageFolder folder)
    {
        foreach (Windows.Storage.StorageFile f in await ReadFiles(folder, ".ms"))
        {
            FilesBlock.Text += "  " + f.Name + "\n";
        }
    }
    
    internal async System.Threading.Tasks.Task<IReadOnlyList<Windows.Storage.StorageFile>> 
        ReadFiles(Windows.Storage.StorageFolder folder, string fileExtension)
    {
        var options = new Windows.Storage.Search.QueryOptions();
        options.FileTypeFilter.Add(fileExtension);
        var query = folder.CreateFileQueryWithOptions(options);
        var files = await query.GetFilesAsync();
    
        return files;
    }
    
    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
    

6. Build and run the app

  1. Press F5 to build and deploy the app (in debug mode).
  2. 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 using an autorun.inf file. You then created an app that registers as a handler for that custom event.

Auto-launching with AutoPlay

Roadmap for Windows Runtime apps using C# or Visual Basic