Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

vb.net - Accessing an MTP/WPD device in Visual Basic .NET

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

enter image description here

Works on My MachineTM

Again, to be clear, this is not my work. I mainly compiled it to DLL.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...