在Visual Basic .NET中访问MTP设备 [英] Accessing an MTP device in Visual Basic .NET
问题描述
迄今为止所见过的一切(在Stack Overflow,Microsoft网站上或使用简单的Google搜索)是使用C ++,C#或其他一些不是VB.net的语言。 Microsoft文档完全在我的头上。
因此,除非我计划学习新的语言,否则这些都不会启动。
我确实发现了 MTPSharp ,这给了我希望。然而,没有文档,似乎没有被完全实现,我尝试做某些事情返回一个例外,作者告诉我,它是针对一个旧的API我不应该使用,无法帮助我
对于想要使用VB.net的人来说真的没有希望吗?
Window Portable Device Class Lib
注意:这是Christophe Geers 从一系列可以在这里找到的博客
$ b $我主要添加了一些功能,一个VB控制台测试,并将其转换为一个Class Lib。我在代码中调整了一些东西来简化它,但不值得进一步提及。
文档:
研究 。
Visual Studio的IntelliSense在识别可用的属性和方法方面也很有价值。
重要注意事项
注意事项
我有很少的便携式设备(并且找不到),所以测试相当有限。
文件和文件夹
术语<$ c $在这种情况下,c>文件和文件夹
可能会导致误导。
如文章所示,有一个 PortableDeviceObject
类型(类), PortableDeviceFile
和 PortableDeviceFolder
继承。 PortableDeviceObject
具有名为 Files
的属性集合,但该集合实际上包含 PortableDeviceObject
秒。该集合中的任何一个项目实际上可能是另一个文件夹。
我也开始实现一个文件夹集合,然后弄清楚为什么它是这样的。由于文件夹可以包含子文件夹,将文件与子文件夹链接到PortableDevice的文件夹会更加令人困惑和困难。所以我离开了
这意味着 Files
集合中的每个项目都必须进行测试,以确定它是否真的是文件
或文件夹
。这通常可以通过两种方式之一进行:
'使用VB运算符
如果TypeOf项是PortableDeviceFolder然后
Console.Beep()
End If
'using NET Type method
如果item.GetType是GetType(PortableDeviceFolder)然后
Console.Beep()
结束如果
使事情稍微更简单和更面向对象,我将 IsFile
和 IsFolder
函数添加到 PortableDeviceObject
允许:
如果item.IsFolder然后
DisplayFolderContents(dev,CType(item,PortableDeviceFolder))
结束如果
还有一种方法返回一个 ItemType
枚举值(还有一个静态版本可能是有用的):
'使用GetItemType
如果item.GetItemType = PortableDeviceObject.ItemTypes.File然后
Console.Beep()
End If
资源
Geers先生原始来源
MSDN Windows便携式设备文档更多的信息,当你准备制作mods以后。
一个VB控制台应用程序(只是一个翻译)显示如何使用一些功能。研究博客fr的详细信息。
代码很长,将大大复制Geers博客的博客,我不愿意发布不是我的代码。此外,如果您无法将其编译为DLL,C#代码显然会很好,所以,为了回答提出的问题,请问有没有可用的VB.net可以让我轻松访问MTP设备?
是。修改后的源代码项目文件(VS2012),一个新的VB控制台测试应用程序和二进制文件( PortableDevices.dll
)可以是下载href =https://www.dropbox.com/s/8hfctmxsy0j75ft/Windows%20Portable%20Devices.zip?dl=0 =nofollow noreferrer。 bin / compile文件夹包括AnyCPU / Release和x86 / Release的构建
- 我想你会想使用
PortableDevice.DLL
保留位于这些文件夹中的Interop。*
。例如,将它们与DLL一起复制到您的工具目录。我不知道为什么他这样做。 - 要在项目中使用新的类Lib,您显然需要添加对您的全新
PortableDevice.DLL
。
当然,使用项目源文件,您可以加载它并重新编译到任何你想要的格式VS编译C#项目的方式与VB中的相同。
在我的机器上运行 TM
再次,要明确,这不是我的工作。我主要将其编译为DLL。
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.
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.
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.
Is there really no hope for someone who wants to use VB.net?
A Window Portable Device Class Lib
Note: this is the work of Christophe Geers from a series of blogs which can be found here
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.
Documentation:
Study Mr Geers' blog.
Visual Studio's IntelliSense will also be valuable in identifying the properties and methods available.
Important Notes
Caveat
I have very few portable devices (and cant find one), so testing was rather limited.
Files and Folders
The term File
and Folder
in this context can be misleading.
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.
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.
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
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
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
Resources
Mr Geers' original source
Another C# Project for WPD which may be useful
MSDN Windows Portable Devices documentation for more information when you get ready to make mods later.
A VB Console app (just a translation) shows how to use some of the functions. Study the blog fr details.
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?:
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
- 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
.
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.
Works on My MachineTM
Again, to be clear, this is not my work. I mainly compiled it to DLL.
这篇关于在Visual Basic .NET中访问MTP设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!