Wednesday 22 March 2023

Is possible to use a 'DeviceWatcher' in a WinForms?

 Yes, it is possible, provided you are running on Win 8 / Win Server 2012.

Scott Hanselman has a nice article on how to call WinRT methods from a desktop app.

The basics of it are, add the following to your project file (Unload it, edit it, reload it):

<PropertyGroup>
    <TargetPlatformVersion>8.0</TargetPlatformVersion>
</PropertyGroup>

Then add a reference to C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\Facades\System.Runtime.InteropServices.WindowsRuntime.dll

You also need to add references to the Windows.Devices and Windows.Foundation through the Add References Dialog under the Windows tab:

enter image description here

Once you do that, you can instantiate the Watcher and add event handlers:

DeviceWatcher dw = Windows.Devices.Enumeration.DeviceInformation.CreateWatcher();

dw.Added += dw_Added;
dw.Removed += dw_Removed;

dw.Start();

No comments:

Post a Comment