資訊
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

DeviceInformation Class

表示裝置。 這個類別允許存取已知的裝置屬性,以及其他在裝置列舉期間所指定的屬性。

語法


var deviceInformation = Windows.Devices.Enumeration.DeviceInformation;

屬性

[DualApiPartition()]
[MarshalingBehavior(Agile)]
[Threading(Both)]
[Version(0x06020000)]

成員

DeviceInformation類別 具有下列類型的成員:

方法

The DeviceInformation 類別 擁有這些方法。 使用 C#、Visual Basic 和 C++,它也會繼承方法自Object 類別.

方法描述
CreateFromIdAsync(String) Creates a DeviceInformation object from a DeviceInformation ID.
CreateFromIdAsync(String, IIterable(String)) Creates a DeviceInformation object from a DeviceInformation ID and a list of additional properties.
CreateWatcher() Creates a DeviceWatcher for all devices.
CreateWatcher(DeviceClass) Creates a DeviceWatcher for devices matching the specified DeviceClass.
CreateWatcher(String) Creates a DeviceWatcher for devices matching the specified Advanced Query Syntax (AQS) string.
CreateWatcher(String, IIterable(String)) Creates a DeviceWatcher for devices matching the specified Advanced Query Syntax (AQS) string and the specified collection of properties.
FindAllAsync() Enumerates all DeviceInformation objects.
FindAllAsync(DeviceClass) Enumerates DeviceInformation objects of the specified class.
FindAllAsync(String) Enumerates DeviceInformation objects matching the specified Advanced Query Syntax (AQS) string.
FindAllAsync(String, IIterable(String)) Enumerates DeviceInformation objects matching the specified Advanced Query Syntax (AQS) string and including the specified collection of properties.
GetGlyphThumbnailAsync Gets a glyph for the device.
GetThumbnailAsync Returns a thumbnail image for the device.
Update Updates the properties of an existing DeviceInformation object.

 

屬性

DeviceInformation類別 擁有這些屬性。

屬性存取類型描述

EnclosureLocation

唯讀The physical location of the device in its enclosure.

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.

Name

唯讀The name of the device.

Properties

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

 

備註

FindAllAsync 成功完成會產生包含 DeviceInformation 物件的 DeviceInformationCollection

如果呼叫 CreateWatcher 成功,就會將 DeviceInformation 物件傳遞至所找到每個裝置的 added 事件。

Name 屬性應該只用於顯示用途,而不是用來尋找裝置,因為 Name 可能會因為當地語系化或使用者指派名稱的原因而變更。

如果成功的話,CreateFromIdAsync 會建立 DeviceInformation 物件。

DeviceInformation 類別提供裝置資訊,更精確地說,它提供裝置介面 (即表示裝置公開之功能的介面) 的屬性。多功能裝置可能有多個裝置介面。使用者視為裝置的實體物件稱為裝置容器,並具有屬性,例如 ManufacturerModelID。 如需與裝置介面、裝置容器及其他隨插即用物件相關聯的屬性清單,請參閱如何擷取裝置或隨插即用物件的其他屬性中的標準屬性清單。如需如何使用 DeviceInformation 類別取得裝置資訊的教學課程,請參閱如何擷取裝置或隨插即用物件的其他屬性、如何擷取相關的隨插即用物件快速入門:列舉裝置容器

範例

這個範例會以累加方式列舉裝置,並在每次找到裝置時,將它們加入至清單,同時處理移除和更新。


    var watcher;
    var isEnumerationComplete = false;
    var deviceArray = new Array(); // Saves the enumeration results

    function WatchDevices() {
        try {
            output.innerHTML = ""; // clear output field

            watcher = 
                Windows.Devices.Enumeration.DeviceInformation.createWatcher();
            // Add event handlers
            watcher.addEventListener("added", onAdded);
            watcher.addEventListener("removed", onRemoved);
            watcher.addEventListener("updated", onUpdated);
            watcher.addEventListener("enumerationcompleted", 
                onEnumerationCompleted);
            watcher.addEventListener("stopped", onStopped);
            // Start enumerating and listening for events
            watcher.start();
        } catch (e) {
            document.getElementById("statusMessage").innerHTML = 
                "Failed to create watcher, error: " + e.message;
        }
    }

    function stopWatcher() {
        try {
            watcher.stop();
        }
        catch (e) {
            document.getElementById("statusMessage").innerHTML = 
                "Failed to stop watcher: " + e.message;
        }
    }

    function onAdded(devinfo) {
        document.getElementById("output").innerHTML += "<p>Device added: " + 
            devinfo.name + "</p>";
        deviceArray.push(devinfo);
        if (isEnumerationComplete) {
            output.innerHTML = ""; // clear output field
            printDeviceArray(document.getElementById("output"));
        }
        
    }

    function onUpdated(devUpdate) {
        document.getElementById("output").innerHTML += "<p>Device updated.</p>";
        for (var i = 0; i < deviceArray.length; i++) {
            if (deviceArray[i].id == devUpdate.id) {
                deviceArray[i].update(devUpdate);
            }
        }
        output.innerHTML = ""; // clear output field
        printDeviceArray(document.getElementById("output"));
    }

    function onRemoved(devupdate) {
        document.getElementById("output").innerHTML += "<p>Device removed.</p>";
        for (var i = 0; i < deviceArray.length; i++) {
            if (deviceArray[i].id == devupdate.id) {
                deviceArray[i].slice(devupdate);
            }
        }
        output.innerHTML = ""; // clear output field
        printDeviceArray(document.getElementById("output"));
    }

    function onEnumerationCompleted(obj) {
        isEnumerationComplete = true;
        document.getElementById("output").innerHTML += 
            "<p>Enumeration Completed.</p>";
        printDeviceArray(document.getElementById("output"));
    }

    function onStopped(obj) {
        document.getElementById("output").innerHTML += "<p>Stopped.</p>";
        if (watcher.status == Windows.Devices.Enumeration.DeviceWatcherStatus.aborted) {
           document.getElementById("output").innerHTML += 
            "<p>Enumeration stopped unexpectedly. </p>";
           document.getElementById("output").innerHTML += 
            "<p>Click the Watch button to restart enumeration.</p>";
        } else if (watcher.status == Windows.Devices.Enumeration.DeviceWatcherStatus.stopped) {
           document.getElementById("output").innerHTML += 
            "<p>You requested to stop enumeration. </p>";
           document.getElementById("output").innerHTML += 
            "<p>Click the Watch button to restart enumeration.</p>";
        }

    }


    // Prints the friendly name of the device interface, 
    // its ID (device interface path), and whether it is enabled.
    function printDevice(deviceInterface, outputDestination) {
        outputDestination.innerHTML += "<p>Name: " + 
            deviceInterface.name + "<p/>"; 
        outputDestination.innerHTML += "<p>Interface ID: " + 
            deviceInterface.id + "<p/>";    
        outputDestination.innerHTML += "<p>Enabled: " + 
            deviceInterface.isEnabled + "<p/>";
        outputDestination.innerHTML += "<br />";
    }

    function printDeviceArray(outputDestination) {
        for (var i = 0; i < deviceArray.length; i++) {
            printDevice(deviceArray[i], outputDestination);
        }
    }


需求

支援的最小用戶端

Windows 8 [Windows 市集應用程式, 桌面應用程式]

支援的最小伺服器

Windows Server 2012 [Windows 市集應用程式, 桌面應用程式]

命名空間

Windows.Devices.Enumeration
Windows::Devices::Enumeration [C++]

Metadata

Windows.winmd

 

 

顯示:
© 2015 Microsoft