4

I was trying to found the most efficient and maybe implemented way in .NET Framework Classes to monitorice drives, actually I know how to do this P/invoking, using structures, etc... but it's a lot of code and I wanted to improve it.

So I've found this interesting Class, DeviceWatcher, which seems to be able only for Metro apps?

I can't find much info about that class and I would like to know if from a Winforms maybe referencing the needed dll I could instance this class to use it in a Winforms?

ElektroStudios
  • 19,105
  • 33
  • 200
  • 417

2 Answers2

5

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();
Community
  • 1
  • 1
John Koerner
  • 37,428
  • 8
  • 84
  • 134
  • seems awesome but I followed the steps and to be able to manage the devicewatcher events (added, removed) It needs more work than that 'cause it throws a compilation error that says `Reference required to assembly Windows.Foundation version 255.255.255.255 containing the definition for event DeviceWatcher.Added`, but the windows searcher didn't found any dll with that name 'Windows.Foundation' on my PC, then what more I'm missing and which is supposed to be the full path of that assembly?, I'm running on a Windows 8.1 with FW 4/4.5/4.5.1 and VS2013 Ultimate installed. – ElektroStudios Feb 18 '14 at 02:28
  • @ElektroStudios I updated the answer with additional references that are necessary. – John Koerner Jul 23 '14 at 13:07
  • It also needs to reference: "C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\Facades\System.Runtime.dll", so are 4 dll's in total (not 3): Runtime, WindowsRuntime, Windows.Devices and Windows.Foundation – ElektroStudios Jul 24 '14 at 18:07
1

So basically these are the proper steps:

  1. Create a new 'WinForms' project targeting .NET Framework 4.5.

  2. Close VisualStudio, open the "YourProjectName.vbproj" file in a text-editor and add this property:

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

3.Load the project in VisualStudio, open the 'References' menu and add these references:

C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework\v4.5\Facades\System.Runtime.dll

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

4.In the 'References' menu, go to "Windows > Core" tab and add these references:

Windows.Devices

Windows.Foundation


Now you will be able to perform this:

Public Class DeviceWatcher_Test

    Private WithEvents dw As DeviceWatcher = DeviceInformation.CreateWatcher()

    Private Sub Test() Handles MyBase.Load

        dw.Start()

    End Sub

    Private Sub dw_Added(ByVal sender As DeviceWatcher, ByVal e As DeviceInformation) _
    Handles dw.Added

        Debug.WriteLine("dw_added: " & e.Id & " | " & e.Name)

    End Sub

    Private Sub dw_Removed(ByVal sender As DeviceWatcher, ByVal e As DeviceInformationUpdate) _
    Handles dw.Removed

        Debug.WriteLine("dw_Removed: " & e.Id)

    End Sub

    Private Sub dw_Updated(ByVal sender As DeviceWatcher, ByVal e As DeviceInformationUpdate) _
    Handles dw.Updated

        Debug.WriteLine("dw_Updated: " & e.Id)

    End Sub

    Private Sub dw_Stopped(ByVal sender As DeviceWatcher, ByVal e As Object) _
    Handles dw.Stopped

        Debug.WriteLine("dw_Stopped: " & e.ToString)

    End Sub

    Private Sub dw_EnumerationCompleted(ByVal sender As DeviceWatcher, ByVal e As Object) _
    Handles dw.EnumerationCompleted

        Debug.WriteLine("dw_EnumerationCompleted: " & e.ToString)

    End Sub

End Class
Ňɏssa Pøngjǣrdenlarp
  • 38,411
  • 12
  • 59
  • 178
ElektroStudios
  • 19,105
  • 33
  • 200
  • 417