vb.net 在 Visual Basic .NET 中访问 MTP/WPD 设备
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26514869/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Accessing an MTP/WPD device in Visual Basic .NET
提问by Richard
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.
是否有任何可用于 VB.net 的库使我能够轻松访问 MTP 设备?我希望能够找出连接了哪些设备,列出它们的内容并向它们复制文件和从它们复制文件。
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.
到目前为止,我所看到的所有内容(在 Stack Overflow、Microsoft 网站上或通过简单的 Google 搜索)都是使用 C++、C# 或其他非 VB.net 语言编写的。Microsoft 文档完全超出了我的理解。
As a result, it's all non-starter unless I plan to learn a new language.
结果,除非我打算学习一门新语言,否则一切都不是入门。
I did find MTPSharpwhich 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.
我确实找到了MTPSharp,这给了我希望。但是没有文档,它似乎没有完全实现,我尝试做某些事情会返回一个异常,作者告诉我它是针对我不应该使用的旧 API 编写的,并且无法帮助我解决我有问题。
Is there really no hope for someone who wants to use VB.net?
想要使用VB.net的人真的没有希望吗?
回答by ??ssa P?ngj?rdenlarp
A Window Portable Device Class Lib
一个 Window 便携式设备类库
Note: this is the work of Christophe Geersfrom a series of blogs which can be found here
注意:这是Christophe Geers的一系列博客的作品,可在此处找到
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.
我主要是加了几个函数,一个VB控制台测试,转换成Class Lib。我调整了代码中的一些内容以简化它,但它们不值得进一步提及。
Documentation:
文档:
StudyMr Geers' blog.
研究Geers 先生的博客。
Visual Studio's IntelliSense will also be valuable in identifying the properties and methods available.
Visual Studio 的 IntelliSense 在识别可用的属性和方法方面也很有价值。
Important Notes
重要笔记
Caveat
警告
I have very few portable devices (and cant find one), so testing was rather limited.
我的便携式设备很少(并且找不到),因此测试相当有限。
Files and Folders
文件和文件夹
The term Fileand Folderin this context can be misleading.
术语File和Folder在这种情况下可能会产生误导。
As the article makes clear, there is a PortableDeviceObjecttype (class) from which both PortableDeviceFileand PortableDeviceFolderinherit. PortableDeviceObjecthas 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.
正如文章所说,有一个PortableDeviceObject类型(类),PortableDeviceFile和PortableDeviceFolder都从它继承。 PortableDeviceObject有一个名为 的属性集合Files,但该集合实际上包含PortableDeviceObjects。该集合中的任何一个项目实际上都可能是另一个文件夹。
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 Filescollection must be tested to see whether it is really a Fileor a Folder. This would typically be done one of two ways:
这意味着Files必须测试集合中的每个项目以查看它是否真的是 aFile或 aFolder。这通常可以通过以下两种方式之一完成:
' 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 slightlysimpler and more object oriented, I added an IsFileand IsFolderfunction to PortableDeviceObjectwhich allows:
为了使事情稍微简单一些并且更加面向对象,我添加了一个IsFileandIsFolder函数,PortableDeviceObject它允许:
If item.IsFolder Then
DisplayFolderContents(dev, CType(item, PortableDeviceFolder))
End If
There is also a method which returns an ItemTypeenum value (there is also a static version which may be useful):
还有一个返回ItemType枚举值的方法(还有一个可能有用的静态版本):
' using GetItemType
If item.GetItemType = PortableDeviceObject.ItemTypes.File Then
Console.Beep()
End If
Resources
资源
Mr Geers' original source
吉尔斯先生的原始出处
Another C# Projectfor WPD which may be useful
MSDN Windows Portable Devices documentationfor more information when you get ready to make mods later.
MSDN Windows 便携式设备文档以在您准备稍后制作模组时获取更多信息。
A VB Console app (just a translation) shows how to use some of the functions. Study the blog fr details.
一个 VB 控制台应用程序(只是一个翻译)展示了如何使用一些功能。研究博客 fr 详细信息。
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?:
代码很长,在很大程度上会复制 Geers 先生的博客,而且我不愿意发布不是我的代码。此外,如果您不能将 C# 代码编译为 DLL,那么它显然对您没有任何好处。因此,为了回答提出的问题,是否有任何可用于 VB.net 的库使我能够轻松访问 MTP 设备?:
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
是的。PortableDevices.dll可以从 DropBox 下载修改后的源代码、项目文件 (VS2012)、新的 VB 控制台测试应用程序和二进制文件 ( ) 。bin/compile 文件夹包括 Builds for AnyCPU/Release 和 x86/Release
- I thinkyou 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.
- 我认为您会希望将
Interop.*DLL保存在那些带有PortableDevice.DLL. 例如,将它们与 DLL 一起复制到您的工具目录。我不知道他为什么那样做。 - 要在项目中使用新的类库,您显然需要添加对全新
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.
当然,使用项目源文件,您可以加载它并重新编译为您想要的任何格式。VS 以与在 VB 中相同的方式编译 C# 项目。


Works on My MachineTM
适用于我的机器TM
Again, to be clear, this is not my work. I mainly compiled it to DLL.
再次声明,这不是我的工作。我主要是编译成DLL。
回答by Formalist
(this should be a comment to this answer, but I don't have enough reputation to add comments)
(这应该是对此答案的评论,但我没有足够的声誉来添加评论)
@Richard said:
@Richard 说:
When I run the C# code I get an error at line 107 this._device.Content(out content); "Object reference not set to an instance of an object.".
当我运行 C# 代码时,在第 107 行出现错误 this._device.Content(out content); “你调用的对象是空的。”。
Adding the line:
添加行:
this.Connect();
before line 107 solved the problem for me.
在第 107 行之前为我解决了这个问题。
回答by Formalist
This MediaDevicesnuget package should suit you.
这个MediaDevicesnuget 包应该适合你。
From package description:
从包描述:
API to communicate with MTP / WPD devices like smart phones, tablets and cameras. Documentation included.
与智能手机、平板电脑和相机等 MTP / WPD 设备通信的 API。包括文档。
(GitHub link).
(GitHub 链接)。
To open a device and write a file on it:
要打开设备并在其上写入文件:
var devices = MediaDevice.GetDevices();
using (var device = devices.First(d => d.FriendlyName == "My Cell Phone"))
{
device.Connect();
device.CreateDirectory(@"\Phone\Documents\Temp");
using(FileStream stream = File.OpenRead(@"C:/Temp/Test.txt"))
{
device.UploadFile(stream, @"\Phone\Documents\Temp\Test.txt");
}
device.Disconnect();
}

