能够在一个WinForms使用一个'DeviceWatcher'?WinForms、DeviceWatcher

2023-09-03 04:01:20 作者:孤寡.

我试图找到在.NET框架类最有效,最可能实现的方式monitorice驱动器,其实我知道如何做到这一点的P /调用,使用结构,等等...但它的很多$ C $的C和我想改进。

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.

所以,我已经发现了这个有趣的类, DeviceWatcher ,这似乎可以只对地铁的应用程序?

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

我不能找到有关这个类的很多信息,我想知道,如果从一个WinForms也许引用所需的dll我可以实例这个类来使用它在一个WinForms?

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?

推荐答案

是的,这是可能的,只要你是在Win运行8 /赢Server 2012中。

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

在Scott Hanselman有一个很好的如何调用的WinRT方法从一个桌面应用程序文章。

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>

然后添加一个引用 C:\ Program Files文件(x86)的\参考Assemblies\Microsoft\Framework\.NETFramework\v4.5\Facades\System.Runtime.InteropServices.WindowsRuntime.dll

您还需要通过引用添加引用下添加到 Windows.Devices Windows.Foundation 对话在窗口标签:

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