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

妖精的绣舞 提交于 2020-01-01 12:01:30

问题


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?


回答1:


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:

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



回答2:


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


来源:https://stackoverflow.com/questions/21835653/is-possible-to-use-a-devicewatcher-in-a-winforms

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!