我一直在尝试使用 WPD API 在 c# 中编写一个示例应用程序,用于将图像文件传输到连接的 WPD 支持的设备。我一直在关注这个链接。我的问题是每次我尝试传输文件时都会收到错误消息:值不在预期范围内。有没有人尝试过成功地做同样的事情。任何指针都受到高度赞赏。
下面是我面临错误的代码片段
IPortableDeviceContent content;
this._device.Content(out content);
IPortableDeviceValues values =
GetRequiredPropertiesForContentType(fileName, parentObjectId);
PortableDeviceApiLib.IStream tempStream;
uint optimalTransferSizeBytes = 0;
content.CreateObjectWithPropertiesAndData(
values,
out tempStream,
ref optimalTransferSizeBytes,
null);
System.Runtime.InteropServices.ComTypes.IStream targetStream =
(System.Runtime.InteropServices.ComTypes.IStream) tempStream;
try
{
using (var sourceStream =
new FileStream(fileName, FileMode.Open, FileAccess.Read))
{
var buffer = new byte[optimalTransferSizeBytes];
int bytesRead;
do
{
bytesRead = sourceStream.Read(
buffer, 0, (int)optimalTransferSizeBytes);
IntPtr pcbWritten = IntPtr.Zero;
targetStream.Write(
buffer, (int)optimalTransferSizeBytes, pcbWritten);
} while (bytesRead > 0);
}
targetStream.Commit(0);
}
finally
{
Marshal.ReleaseComObject(tempStream);
}
错误出现在 targetStream.Write(... 下面是我设置参数的方式。我认为我正在设置的参数有问题,或者我缺少一些必需的参数。
IPortableDeviceValues values =
new PortableDeviceTypesLib.PortableDeviceValues() as IPortableDeviceValues;
var WPD_OBJECT_PARENT_ID = new _tagpropertykey();
WPD_OBJECT_PARENT_ID.fmtid =
new Guid(0xEF6B490D, 0x5CD8, 0x437A, 0xAF, 0xFC,
0xDA, 0x8B, 0x60, 0xEE, 0x4A, 0x3C);
WPD_OBJECT_PARENT_ID.pid = 3 ;
values.SetStringValue(ref WPD_OBJECT_PARENT_ID, parentObjectId);
FileInfo fileInfo = new FileInfo(fileName);
var WPD_OBJECT_SIZE = new _tagpropertykey();
WPD_OBJECT_SIZE.fmtid =
new Guid(0xEF6B490D, 0x5CD8, 0x437A, 0xAF, 0xFC,
0xDA, 0x8B, 0x60, 0xEE, 0x4A, 0x3C);
WPD_OBJECT_SIZE.pid = 11;
values.SetUnsignedLargeIntegerValue(WPD_OBJECT_SIZE, (ulong) fileInfo.Length);
var WPD_OBJECT_ORIGINAL_FILE_NAME = new _tagpropertykey();
WPD_OBJECT_ORIGINAL_FILE_NAME.fmtid =
new Guid(0xEF6B490D, 0x5CD8, 0x437A, 0xAF, 0xFC,
0xDA, 0x8B, 0x60, 0xEE, 0x4A, 0x3C);
WPD_OBJECT_ORIGINAL_FILE_NAME.pid = 12;
values.SetStringValue(WPD_OBJECT_ORIGINAL_FILE_NAME, Path.GetFileName(fileName));
var WPD_OBJECT_NAME = new _tagpropertykey();
WPD_OBJECT_NAME.fmtid =
new Guid(0xEF6B490D, 0x5CD8, 0x437A, 0xAF, 0xFC,
0xDA, 0x8B, 0x60, 0xEE, 0x4A, 0x3C);
WPD_OBJECT_NAME.pid = 4;
values.SetStringValue(WPD_OBJECT_NAME, Path.GetFileName(fileName));
var WPD_OBJECT_FORMAT = new _tagpropertykey();
WPD_OBJECT_FORMAT.fmtid = new Guid(0xef2107d5, 0xa52a, 0x4243, 0xa2, 0x6b, 0x62, 0xd4, 0x17, 0x6d, 0x76, 0x03);
WPD_OBJECT_FORMAT.pid = 6;
values.SetGuidValue(WPD_OBJECT_FORMAT, WPD_OBJECT_FORMAT.fmtid);