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?

有帮助吗?

解决方案

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();

其他提示

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
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top