我正在尝试让应用程序接受从 Gmail 网页拖入的附件。
但是当我直接从站点拖入附件时,e.Data 似乎也不包含任何数据。
private void Form1_DragDrop(object sender, DragEventArgs e)
{
string[] dataFormats = e.Data.GetFormats();
Type type = e.Data.GetType();
}
e.Data 是 DataObject 类型。 FileDrop、FileNameW、FileName 为空 DragContext、DragImageBits、chromium/x-renderer-taint 是 System.IO.MemoryStream
没有一个 Memorystream 对象包含拖入的附件的任何文件数据。 也没有任何下载网址。
编辑。 显然,当拖入图片附件时,数据中包含 URL。 但其他附件不带 URL 但是当我将它拖到桌面时,Windows 资源管理器会以某种方式知道从哪里下载它,因此必须有一种方法来检索此 URL。
编辑2 添加了使用 DataObject 的立即窗口查看的数据
(e.Data as System.Windows.DataObject).GetFormats(false);
{string[4]}
[0]: "DragContext"
[1]: "DragImageBits"
[2]: "chromium/x-renderer-taint"
[3]: "FileDrop"
(e.Data as System.Windows.DataObject).GetData("DragContext");
'(e.Data as System.Windows.DataObject).GetData("DragContext")' threw an exception of type 'System.Runtime.InteropServices.COMException'
Data: {System.Collections.ListDictionaryInternal}
ErrorCode: -2147221404
HResult: -2147221404
HelpLink: null
InnerException: null
Message: "Invalid FORMATETC structure (Exception from HRESULT: 0x80040064 (DV_E_FORMATETC))"
Source: "System"
StackTrace: " at System.Runtime.InteropServices.ComTypes.IDataObject.GetData(FORMATETC& format, STGMEDIUM& medium)\r\n at System.Windows.DataObject.OleConverter.GetDataInner(FORMATETC& formatetc, STGMEDIUM& medium)\r\n at System.Windows.DataObject.OleConverter.GetDataFromOleHGLOBAL(String format, DVASPECT aspect, Int32 index)\r\n at System.Windows.DataObject.OleConverter.GetDataFromBoundOleDataObject(String format, DVASPECT aspect, Int32 index)\r\n at System.Windows.DataObject.OleConverter.GetData(String format, Boolean autoConvert, DVASPECT aspect, Int32 index)\r\n at System.Windows.DataObject.OleConverter.GetData(String format, Boolean autoConvert)\r\n at System.Windows.DataObject.GetData(String format, Boolean autoConvert)\r\n at System.Windows.DataObject.GetData(String format)"
TargetSite: {Void GetData(System.Runtime.InteropServices.ComTypes.FORMATETC ByRef, System.Runtime.InteropServices.ComTypes.STGMEDIUM ByRef)}
(e.Data as System.Windows.DataObject).GetData("DragImageBits");
{System.IO.MemoryStream}
CanRead: true
CanSeek: true
CanTimeout: false
CanWrite: true
Capacity: 87144
Length: 87144
Position: 0
ReadTimeout: '((System.IO.Stream)(e.Data as System.Windows.DataObject).GetData("DragImageBits")).ReadTimeout' threw an exception of type 'System.InvalidOperationException'
WriteTimeout: '((System.IO.Stream)(e.Data as System.Windows.DataObject).GetData("DragImageBits")).WriteTimeout' threw an exception of type 'System.InvalidOperationException'
(e.Data as System.Windows.DataObject).GetData("chromium/x-renderer-taint");
{System.IO.MemoryStream}
CanRead: true
CanSeek: true
CanTimeout: false
CanWrite: true
Capacity: 1
Length: 1
Position: 0
ReadTimeout: '((System.IO.Stream)(e.Data as System.Windows.DataObject).GetData("chromium/x-renderer-taint")).ReadTimeout' threw an exception of type 'System.InvalidOperationException'
WriteTimeout: '((System.IO.Stream)(e.Data as System.Windows.DataObject).GetData("chromium/x-renderer-taint")).WriteTimeout' threw an exception of type 'System.InvalidOperationException'
(e.Data as System.Windows.DataObject).GetData("FileDrop");
'(e.Data as System.Windows.DataObject).GetData("FileDrop")' threw an exception of type 'System.Runtime.InteropServices.COMException'
Data: {System.Collections.ListDictionaryInternal}
ErrorCode: -2147221404
HResult: -2147221404
HelpLink: null
InnerException: null
Message: "Invalid FORMATETC structure (Exception from HRESULT: 0x80040064 (DV_E_FORMATETC))"
Source: "System"
StackTrace: " at System.Runtime.InteropServices.ComTypes.IDataObject.GetData(FORMATETC& format, STGMEDIUM& medium)\r\n at System.Windows.DataObject.OleConverter.GetDataInner(FORMATETC& formatetc, STGMEDIUM& medium)\r\n at System.Windows.DataObject.OleConverter.GetDataFromOleHGLOBAL(String format, DVASPECT aspect, Int32 index)\r\n at System.Windows.DataObject.OleConverter.GetDataFromBoundOleDataObject(String format, DVASPECT aspect, Int32 index)\r\n at System.Windows.DataObject.OleConverter.GetData(String format, Boolean autoConvert, DVASPECT aspect, Int32 index)\r\n at System.Windows.DataObject.OleConverter.GetData(String format, Boolean autoConvert)\r\n at System.Windows.DataObject.GetData(String format, Boolean autoConvert)\r\n at System.Windows.DataObject.GetData(String format)"
TargetSite: {Void GetData(System.Runtime.InteropServices.ComTypes.FORMATETC ByRef, System.Runtime.InteropServices.ComTypes.STGMEDIUM ByRef)}
最佳答案
附件被检测为 FileDrop,数据包含附件的 URL,可以通过执行 GetData(DataFormats.Text) 检索。
这里有一些示例代码供您尝试:
private void Form1_DragDrop(object sender, DragEventArgs e) {
string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
if (files == null) {
string url = (string)e.Data.GetData(DataFormats.Text);
MessageBox.Show(url);
}
}
private void Form1_DragEnter(object sender, DragEventArgs e) {
if (e.Data.GetDataPresent(DataFormats.FileDrop)) {
e.Effect = DragDropEffects.Copy;
}
}
请注意,处理 DragEnter 事件并将 e.Effect 设置为 DragDropEffects.Copy 很重要,否则 DragDrop 事件将不会触发,或者不会有您需要的数据。
关于c# - 将 Gmail 附件拖到 c# winform,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48849694/