Udostępnij za pośrednictwem


DeviceInformation Class

Definition

Represents a device. This class allows access to well-known device properties as well as additional properties specified during device enumeration.

public ref class DeviceInformation sealed
/// [Windows.Foundation.Metadata.ContractVersion(Windows.Foundation.UniversalApiContract, 65536)]
/// [Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
/// [Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
class DeviceInformation final
[Windows.Foundation.Metadata.ContractVersion(typeof(Windows.Foundation.UniversalApiContract), 65536)]
[Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
[Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
public sealed class DeviceInformation
Public NotInheritable Class DeviceInformation
Inheritance
Object Platform::Object IInspectable DeviceInformation
Attributes

Windows requirements

Device family
Windows 10 (introduced in 10.0.10240.0)
API contract
Windows.Foundation.UniversalApiContract (introduced in v1.0)

Examples

This example incrementally enumerates devices, adding them to a list each time a device is found, and also handling removals and updates.

using System;
using System.Threading.Tasks;
using System.Collections.Generic;
using System.IO;
using System.Diagnostics;
using System.Linq;
using System.Text;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using Windows.UI.Xaml.Media.Imaging;

using Windows.Devices.Enumeration;
using Windows.Devices.Enumeration.Pnp;


// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238

namespace Application1
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    /// 
    public sealed partial class BlankPage : Page
    {
        public BlankPage()
        {

            this.InitializeComponent();
        }
        Windows.UI.Core.CoreDispatcher dispatcher;
        public static DeviceWatcher watcher = null;
        public static int count = 0;
        public static DeviceInformation[] interfaces = new DeviceInformation[1000];
        public static bool isEnumerationComplete = false;
        public static string StopStatus = null;

        async void WatchDevices(object sender, RoutedEventArgs eventArgs)
        {
            try
            {
                dispatcher = Window.Current.CoreWindow.Dispatcher;
                watcher = DeviceInformation.CreateWatcher();
                // Add event handlers
                watcher.Added += watcher_Added;
                watcher.Removed += watcher_Removed;
                watcher.Updated += watcher_Updated;
                watcher.EnumerationCompleted += watcher_EnumerationCompleted;
                watcher.Stopped += watcher_Stopped;
                watcher.Start();
                OutputText.Text = "Enumeration started.";

            }
            catch (ArgumentException)
            {
                //The ArgumentException gets thrown by FindAllAsync when the GUID isn't formatted properly
                //The only reason we're catching it here is because the user is allowed to enter GUIDs without validation
                //In normal usage of the API, this exception handling probably wouldn't be necessary when using known-good GUIDs 
                OutputText.Text = "Caught ArgumentException. Failed to create watcher.";
            }
        }

        async void StopWatcher(object sender, RoutedEventArgs eventArgs)
        {
            try
            {
                if (watcher.Status == Windows.Devices.Enumeration.DeviceWatcherStatus.Stopped)
                {
                    StopStatus = "The enumeration is already stopped.";
                }
                else
                {
                    watcher.Stop();
                }
            }
            catch (ArgumentException)
            {
                OutputText.Text = "Caught ArgumentException. Failed to stop watcher.";
            }
        }

        async void watcher_Added(DeviceWatcher sender, DeviceInformation deviceInterface)
        {
            interfaces[count] = deviceInterface;
            count += 1;
            if (isEnumerationComplete)
            {
                await dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
                {
                    DisplayDeviceInterfaceArray();
                });
            }
        }

        async void watcher_Updated(DeviceWatcher sender, DeviceInformationUpdate devUpdate)
        {
            int count2 = 0;
            foreach (DeviceInformation deviceInterface in interfaces)
            {
                if (count2 < count)
                {
                    if (interfaces[count2].Id == devUpdate.Id)
                    {
                        //Update the element.
                        interfaces[count2].Update(devUpdate);
                    }

                }
                count2 += 1;
            }
            await dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
            {
                OutputText.Text = "Enumeration updated. ";
                DisplayDeviceInterfaceArray();
            });
        }

        async void watcher_Removed(DeviceWatcher sender, DeviceInformationUpdate devUpdate)
        {
            int count2 = 0;
            //Convert interfaces array to a list (IList).
            List<DeviceInformation> interfaceList = new List<DeviceInformation>(interfaces);
            foreach (DeviceInformation deviceInterface in interfaces)
            {
                if (count2 < count)
                {
                    if (interfaces[count2].Id == devUpdate.Id)
                    {
                        //Remove the element.
                        interfaceList.RemoveAt(count2);
                    }

                }
                count2 += 1;
            }
            //Convert the list back to the interfaces array.
            interfaces = interfaceList.ToArray();
            count -= 1;
            await dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
            {
                OutputText.Text = "Enumeration device was removed. ";
                DisplayDeviceInterfaceArray();
            });
        }

        async void watcher_EnumerationCompleted(DeviceWatcher sender, object args)
        {
            isEnumerationComplete = true;
            await dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
                {
                    OutputText.Text = "Enumeration complete. ";
                    DisplayDeviceInterfaceArray();
                });
        }

        async void watcher_Stopped(DeviceWatcher sender, object args)
        {
            if (watcher.Status == Windows.Devices.Enumeration.DeviceWatcherStatus.Aborted)
            {
                StopStatus = "Enumeration stopped unexpectedly. Click Watch to restart enumeration.";
            }
            else if (watcher.Status == Windows.Devices.Enumeration.DeviceWatcherStatus.Stopped)
            {
                StopStatus = "You requested to stop the enumeration. Click Watch to restart enumeration.";
            }
        }

        async void DisplayDeviceInterfaceArray()
        {
            DeviceInterfacesOutputList.Items.Clear();
            int count2 = 0;
            foreach (DeviceInformation deviceInterface in interfaces)
            {
                if (count2 < count)
                {
                    DisplayDeviceInterface(deviceInterface);
                }
                count2 += 1;
            }
        }

        async void DisplayDeviceInterface(DeviceInformation deviceInterface)
        {
            var id = "Id:" + deviceInterface.Id;
            var name = deviceInterface.Name;
            var isEnabled = "IsEnabled:" + deviceInterface.IsEnabled;


            var item = id + " is \n" + name + " and \n" + isEnabled;

            DeviceInterfacesOutputList.Items.Add(item);
        }
    }
}

Remarks

A DeviceInformation object is composed of an identity (DeviceInformation.Id), a kind (DeviceInformation.Kind), and a property bag (DeviceInformation.Properties). All of the other properties of a DeviceInformation object are derived from the Properties property bag. For example, Name is derived from System.ItemNameDisplay.

Successful completion of FindAllAsync results in a DeviceInformationCollection containing DeviceInformation objects.

If a call to CreateWatcher succeeds, a DeviceInformation object is passed to the added event for each device that is found.

The Name property should only be used for display purposes only and not for finding a device because the Name can change due to localization or a user assigning a name.

CreateFromIdAsync creates a DeviceInformation object if successful.

The DeviceInformation class provides device information, but more specifically, it provides properties of the device interface, the interface that represents functionality that the device exposes. Multi-function devices may have more than one device interface. The physical object that a user sees as a device, is known as the device container, and has properties such as Manufacturer and ModelID. For more information about enumerating devices and recovering properties, see Enumerate devices.

Properties

EnclosureLocation

The physical location of the device in its enclosure. For example, it may describe the location of a webcam inside a laptop.

Id

A string representing the identity of the device.

IsDefault

Indicates whether this device is the default device for the class.

IsEnabled

Indicates whether this device is enabled.

Kind

Gets the type of DeviceInformation represented by this object.

Name

The name of the device. This name is in the best available language for the app.

Pairing

Gets the information about the capabilities for this device to pair.

Properties

Property store containing well-known values as well as additional properties that can be specified during device enumeration.

Methods

CreateFromIdAsync(String, IIterable<String>, DeviceInformationKind, IDeviceEnumerationSettings)

Asynchronously creates a DeviceInformation object from a DeviceInformation ID, a list of additional properties, a DeviceInformationKind, and a settings object.

CreateFromIdAsync(String, IIterable<String>, DeviceInformationKind)

Creates a DeviceInformation object from a DeviceInformation ID, a list of additional properties, and a DeviceInformationKind parameter.

CreateFromIdAsync(String, IIterable<String>)

Creates a DeviceInformation object from a DeviceInformation ID and a list of additional properties.

CreateFromIdAsync(String)

Creates a DeviceInformation object from a DeviceInformation ID.

CreateWatcher()

Creates a DeviceWatcher for all devices.

CreateWatcher(DeviceClass)

Creates a DeviceWatcher for devices matching the specified DeviceClass.

CreateWatcher(String, IIterable<String>, DeviceInformationKind, IDeviceEnumerationSettings)

Creates a DeviceWatcher for devices matching the specified Advanced Query Syntax (AQS) string, the specified collection of properties, the kind of device, and the settings.

CreateWatcher(String, IIterable<String>, DeviceInformationKind)

Creates a DeviceWatcher for devices matching the specified Advanced Query Syntax (AQS) string, the specified collection of properties, and the kind of devices.

CreateWatcher(String, IIterable<String>)

Creates a DeviceWatcher for devices matching the specified Advanced Query Syntax (AQS) string and the specified collection of properties.

CreateWatcher(String)

Creates a DeviceWatcher for devices matching the specified Advanced Query Syntax (AQS) string.

FindAllAsync()

Enumerates all DeviceInformation objects.

FindAllAsync(DeviceClass)

Enumerates DeviceInformation objects of the specified class.

FindAllAsync(String, IIterable<String>, DeviceInformationKind, IDeviceEnumerationSettings)

Asynchronously enumerates DeviceInformation objects matching the specified Advanced Query Syntax (AQS) device interface selector string, the device kind, including the specified collection of properties, and with the settings.

FindAllAsync(String, IIterable<String>, DeviceInformationKind)

Enumerates DeviceInformation objects matching the specified Advanced Query Syntax (AQS) device interface selector string, the device kind, and including the specified collection of properties.

FindAllAsync(String, IIterable<String>)

Enumerates DeviceInformation objects matching the specified Advanced Query Syntax (AQS) device interface selector string and including the specified collection of properties.

FindAllAsync(String)

Enumerates DeviceInformation objects matching the specified Advanced Query Syntax (AQS) device interface selector string.

GetAqsFilterFromDeviceClass(DeviceClass)

Creates a filter to use to enumerate through a subset of device types.

GetGlyphThumbnailAsync()

Gets a glyph for the device.

GetThumbnailAsync()

Returns a thumbnail image for the device.

Update(DeviceInformationUpdate)

Updates the properties of an existing DeviceInformation object.

Applies to

See also