Accessing an MTP device in Visual Basic .NET

匆匆过客 提交于 2019-12-01 00:31:20

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 PortableDeviceObjects. 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 the PortableDevice.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.

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