在Visual Basic .NET中访问MTP/WPD设备 [英] Accessing an MTP/WPD device in Visual Basic .NET
问题描述
VB.net是否有可用的库使我能够轻松访问MTP设备?我希望能够找出已连接的设备,列出它们的内容,并在它们之间复制文件.
Are there any libraries available for VB.net which will enable me to easily access a MTP device? I'd like to be able to find out what devices are connected, list the contents of them and copy files to and from them.
到目前为止,我所看到的所有内容(无论是在Stack Overflow,在Microsoft网站上,还是通过简单的Google搜索)都是C ++,C#或其他不是VB.net的语言. Microsoft文档完全让我头疼.
Everything I've seen so far (either at Stack Overflow, on the Microsoft site or with a simple Google search) is either in C++, C# or some other language that isn't VB.net. The Microsoft documentation goes completely over my head.
结果,除非我计划学习一种新语言,否则一切都不是初学者.
As a result, it's all non-starter unless I plan to learn a new language.
我确实找到了 MTPSharp ,这给了我希望.但是没有文档,它似乎还没有完全实现,我做某些事情的尝试返回了一个异常,作者告诉我,它是针对我不应该使用的旧API编写的,并且无法帮助我我有问题.
I did find MTPSharp which gave me hope. However there is no documentation, it doesn't appear to be fully implemented, my attempts to do certain things return an exception and the author tells me that it's written against an old API I shouldn't use and is unable to help me with the questions I have.
对于想要使用VB.net的人真的没有希望吗?
Is there really no hope for someone who wants to use VB.net?
推荐答案
窗口便携式设备类库
注意:这是 Christophe Geers 的工作,来自一系列博客,这些博客可能是在此处找到
A Window Portable Device Class Lib
Note: this is the work of Christophe Geers from a series of blogs which can be found here
我主要添加了一些功能,即VB控制台测试,并将其转换为Class Lib.我对代码中的一些内容进行了调整,以简化代码,但是这些内容不值得进一步提及.
I mainly added a few functions, a VB console test, and converted it to a Class Lib. I tweaked a few things in the code to streamline it, but they are not worth further mention.
文档:
研究 盖尔先生博客.
Visual Studio的IntelliSense在识别可用的属性和方法上也将很有价值.
Visual Studio's IntelliSense will also be valuable in identifying the properties and methods available.
注意事项
我的便携式设备很少(找不到一个),因此测试相当有限.
I have very few portable devices (and cant find one), so testing was rather limited.
文件和文件夹
在此上下文中,术语File
和Folder
可能会产生误导.
The term File
and Folder
in this context can be misleading.
正如文章所表明的,PortableDeviceFile
和PortableDeviceFolder
都继承自PortableDeviceObject
类型(类). PortableDeviceObject
有一个名为Files
的属性集合,但是该集合实际上包含PortableDeviceObject
.该集合中的任何一项实际上都可能是另一个文件夹.
As the article makes clear, there is a PortableDeviceObject
type (class) from which both PortableDeviceFile
and PortableDeviceFolder
inherit. PortableDeviceObject
has a property collection named Files
, but that collection actually contains PortableDeviceObject
s. Any one of the items in that collection may in fact be another folder.
我也开始实现Folders集合,然后找出原因.由于文件夹可以包含子文件夹,因此将文件链接到子文件夹再将文件夹链接到PortableDevice会更加混乱和麻烦.所以,我离开了.
I started to implement a Folders collection as well, then figured out why it is the way it is. Since a folder can contain sub-folders, it would be more confusing and problematic to link files to subfolders to folders to a PortableDevice. So, I left it.
这意味着必须对Files
集合中的每个项目进行测试,以查看它是真正的File
还是Folder
.通常,这可以通过以下两种方式之一完成:
This means each item in the Files
collection must be tested to see whether it is really a File
or a Folder
. This would typically be done one of two ways:
' using VB operator
If TypeOf item Is PortableDeviceFolder Then
Console.Beep()
End If
' using NET Type method
If item.GetType Is GetType(PortableDeviceFolder) Then
Console.Beep()
End If
为了使事情简单更简单和更面向对象,我在PortableDeviceObject
中添加了IsFile
和IsFolder
函数,该函数允许:
To make things slightly simpler and more object oriented, I added an IsFile
and IsFolder
function to PortableDeviceObject
which allows:
If item.IsFolder Then
DisplayFolderContents(dev, CType(item, PortableDeviceFolder))
End If
还有一种方法可以返回ItemType
枚举值(还有一个可能有用的静态版本):
There is also a method which returns an ItemType
enum value (there is also a static version which may be useful):
' using GetItemType
If item.GetItemType = PortableDeviceObject.ItemTypes.File Then
Console.Beep()
End If
资源
Geers先生的原始来源
另一个用于WPD的C#项目可能有用
MSDN Windows便携式设备文档以获取更多信息,以备日后制作Mod时使用.
MSDN Windows Portable Devices documentation for more information when you get ready to make mods later.
VB控制台应用程序(仅翻译)显示了如何使用某些功能.研究博客的详细信息.
A VB Console app (just a translation) shows how to use some of the functions. Study the blog fr details.
代码很长,会在很大程度上复制Geers先生的博客,而我不愿意发布不是我的代码.此外,如果您无法将C#代码编译为DLL,则显然对您没有什么帮助.因此,要回答提出的问题, VB.net是否有可用的库使我能够轻松访问MTP设备?:
The code is long, would largely duplicate Mr Geers' blog, and I am disinclined to post code which is not mine. Besides, C# code would apparently do you little good if you can't compile it to a DLL. So, to answer the question posed, Are there any libraries available for VB.net which will enable me to easily access a MTP device?:
是.修改后的源代码,项目文件(VS2012),新的VB控制台测试应用程序和Binaries(PortableDevices.dll
)可以为
Yes. The modified source, project files (VS2012), a new VB console test app and Binaries (PortableDevices.dll
) can be downloaded from DropBox. The bin/compile folders includes Builds for AnyCPU/Release and x86/Release
- 我认为,您将希望使用
PortableDevice.DLL
将Interop.*
DLL保留在这些文件夹中.例如,将它们与DLL一起复制到您的工具目录中.我不确定他为什么这样做. - 要在项目中使用新的类库,显然您需要添加对全新的
PortableDevice.DLL
的引用.
- I think you will want to keep the
Interop.*
DLLs located in those folders with thePortableDevice.DLL
. For instance, copy them both along with the DLL to your tools directory. I am not sure why he did it that way. - To use the new Class Lib in a project, you obviously will need a add a reference to your brand new
PortableDevice.DLL
.
当然,使用项目源文件,您可以加载它并重新编译为所需的任何格式. VS可以像在VB中一样编译C#项目.
Of course, with the project source files you can load it and recompile to whatever format you desire. VS compiles C# projects the same way you do in VB.
在我的机器"上工作 TM
再说一遍,这不是我的工作.我主要将其编译为DLL.
Again, to be clear, this is not my work. I mainly compiled it to DLL.
这篇关于在Visual Basic .NET中访问MTP/WPD设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!