【问题标题】:How to get data from Drag 'n Dropped file that is not saved on disk如何从未保存在磁盘上的拖放文件中获取数据
【发布时间】:2015-07-22 19:15:47
【问题描述】:

如何读取未保存在磁盘任何位置的 xml 文件的 xml 内容?

我希望能够从 Outlook 中拖动附件(文件扩展名是自定义的)并将其拖放到我的应用程序中。根据xml文件的内容,我会做一些相应的动作。

我尝试遍历 e.Data.GetFormats()GetData(format) 但无济于事。 我试过e.Data.GetData("FileContents") 没用。 我也尝试了e.Data.GetData(DataFormat.Text)DataFormats.UnicodeTextDataFormats.FileDrop,但没有任何效果。

DataObject 读取文件的内容很重要,因为我不想强迫用户在拖动文件之前保存它。

任何帮助将不胜感激!

回答

为有相同问题的人提供格式正确的答案:

因此,任何保存在磁盘上的文件都将具有可以加载和读取的完整路径。

从 Outlook 删除的任何文件都将具有“FileGroupDescriptor”以获取文件名及其扩展名。 “FileContents”将包含内容数据流。

例子:

处理拖放的文件,看看我们是否可以做一些动作

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;
    }

【问题讨论】:

    标签: c# xml winforms


    【解决方案1】:

    这是我用来处理将 Windows 资源管理器中的文件或 Outlook 中的附件转换为内存流的代码。 (ms 是表单上的公共内存流变量)。我相信您可以使用相同的逻辑将其转换为字符串阅读器。

          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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-03-29
      • 1970-01-01
      • 2016-11-26
      • 2019-04-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多