如何从未保存在磁盘上的拖放文件中获取数据 [英] How to get data from Drag 'n Dropped file that is not saved on disk
问题描述
如何读取未保存在磁盘任何位置的 xml 文件的 xml 内容?
How can I read xml content of xml file that is not saved anywhere on the disk?
我希望能够从 Outlook 中拖动附件(文件扩展名是自定义的)并将其放入我的应用程序中.根据xml文件的内容,我会做一些相应的动作.
I want to be able to drag an attachment from Outlook (file extension is custom) and drop it in my application. Based on the content of the xml file I will do some Action accordingly.
我尝试遍历 e.Data.GetFormats()
和 GetData(format)
但无济于事.我试过 e.Data.GetData("FileContents")
没有用.我也用 DataFormats.UnicodeText
、DataFormats.FileDrop
尝试了 e.Data.GetData(DataFormat.Text)
,但没有任何效果.
I tried to iterate through e.Data.GetFormats()
and GetData(format)
but no avail.
I tried e.Data.GetData("FileContents")
didn't work.
I tried e.Data.GetData(DataFormat.Text)
also with DataFormats.UnicodeText
, DataFormats.FileDrop
and nothing works.
从 DataObject
读取文件的内容很重要,因为我不想强迫用户在拖动之前保存文件.
It is important to read the content of the file from DataObject
since I don't want to force user to save the file before dragging it.
任何帮助将不胜感激!
为有相同问题的人准备的格式正确的答案:
因此,任何保存在被删除的磁盘上的文件都将具有可以加载和读取的完整路径.
So any file saved on disk being dropped will have full path that can be loaded and read.
从 Outlook 中删除的任何文件都将具有FileGroupDescriptor"获取文件名及其扩展名.文件内容"将包含内容的数据流.
Any file dropped from Outlook will have "FileGroupDesciptor" to get fileName along with its extension. "FileContents" will contain data stream of contents.
示例:
处理拖放的文件,看看我们是否可以做一些动作
public void DragEnter(DragEventArgs e)
{
var obj = e.Data as DataObject;
//get fileName of file saved on disk
var fileNames = obj.GetFileDropList();
if(fileNames.Count > 0)
fileName = fileNames[0]; //I want only one at a time
//Get fileName not save on disk
if (string.IsNullOrEmpty(fileName) && obj.GetDataPresent("FileGroupDescriptor"))
{
var ms = (MemoryStream)obj.GetData("FileGroupDescriptor");
ms.Position = 76;
char a;
while ((a = (char)ms.ReadByte()) != 0)
fileName += a;
}
if (fileName.Length != 0)
{
var extension = fileName.Substring(fileName.LastIndexOf('.') + 1);
switch (extension.ToUpper())
{
case "WTV":
itemType = DropItemType.WTVFile;
break;
default:
itemType = DropItemType.None;
break;
}
canHandleDropData = (itemType != DropItemType.None);
}
if (canHandleDropData)
e.Effect = DragDropEffects.Copy;
}
获取拖放文件的内容
public XmlDocument GetXmlDocument(DragEventArgs dragEventArgs)
{
var doc = new XmlDocument();
//Get content of outlook file not saved on disk
var rawContent = dragEventArgs.Data.GetData("FileContents");
if (rawContent == null)
{
//if null then it is a saved file and I can read its content by loading file name
var xmlString = File.ReadAllText(fileName);
doc.LoadXml(xmlString);
}
else
{
//outlook file content
var xmlString = rawContent as MemoryStream;
doc.Load(xmlString);
}
return doc;
}
推荐答案
这是我使用的代码,用于处理将 Windows 资源管理器中的文件或 Outlook 中的附件转换为内存流.(ms 是表单上的公共内存流变量).我相信您可以使用相同的逻辑将其转换为字符串阅读器.
This is code that I use that handles converting either files from windows explorer or attachments from outlook to a memory stream. (ms is a public memorystream variable on the form). I'm sure you can use the same logic to convert it to a string reader.
Private Sub ubFilepath_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles ubFilepath.DragDrop
Try
If e.Data.GetDataPresent(DataFormats.Text) Then
Me.ubFilepath.Text = e.Data.GetData("Text")
ElseIf e.Data.GetDataPresent(DataFormats.FileDrop) Then
Dim fileNames() As String
Dim MyFilename As String
fileNames = DirectCast(e.Data.GetData(DataFormats.FileDrop), String())
MyFilename = fileNames(0)
Me.ubFilepath.Text = MyFilename
//RenPrivateItem is from outlook
ElseIf e.Data.GetDataPresent("RenPrivateItem") Then
Dim thestream As System.IO.MemoryStream = e.Data.GetData("FileGroupDescriptor")
Dim filename As New System.Text.StringBuilder("")
Dim fileGroupDescriptor(700) As Byte
Try
thestream.Read(fileGroupDescriptor, 0, 700)
Dim i As Integer = 76
While fileGroupDescriptor(i) <> 0
filename.Append(Convert.ToChar(fileGroupDescriptor(i)))
i += 1
End While
Me.ubFilepath.Text = "Outlook attachment_" + filename.ToString
ms = e.Data.GetData("FileContents", True)
Finally
If thestream IsNot Nothing Then thestream.Close()
End Try
End If
Catch ex As Exception
MessageBox.Show(ex.ToString, "Only files can be dragged into this box")
End Try
这篇关于如何从未保存在磁盘上的拖放文件中获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!