将邮件项目拖放到vb.net桌面保存为指定文件夹的文件

求助:

邮件系统中,可以将邮件项目拖放到桌面,自动保存为文件。

问题:

通过vb.net,也实现同样的操作:
将邮件项目拖放到winform桌面(开发程序窗体),自动保存为指定目录下面的文件。

试了一下,代码如下,问题出在ms对象为nothing,无法获取对象。

对如何处理流数据,不太懂。

请高手帮忙、指导一下?[玫瑰][玫瑰]

img

Private Sub 文件名称_DragDrop(sender As Object, e As DragEventArgs) Handles Me.DragDrop

    If e.Data.GetDataPresent("FileGroupDescriptor") Then



        Dim theStream As Stream = DirectCast(e.Data.GetData("FileGroupDescriptor"), Stream)

        Dim fileGroupDescriptor As Byte() = New Byte(512) {}

        theStream.Read(fileGroupDescriptor, 0, 512)



        Dim objFileName As New StringBuilder()



        Dim i As Integer = 76

        While fileGroupDescriptor(i) <> 0

            objFileName.Append(Convert.ToChar(fileGroupDescriptor(i)))

            i += 1

        End While



        Dim strSaveFileName As String = "D:\" + objFileName.ToString()



        Dim ms As System.IO.MemoryStream

        ms = e.Data.GetData("FileContents", True)

        Dim fs As FileStream

        fs = New FileStream(strSaveFileName, FileMode.Create)



        fs.CopyTo(ms)



        fs.Close()

        theStream.Close()



    End If



End Sub

题主有否试过获取拖拽文件所在路径,直接拷贝这个路径的文件就行,我这里测试用Foxmail直接拖到textbox上Foxmail会生成临时文件。没outlook,题主可以自己试试outlook

img

using System;
using System.IO;
using System.Windows.Forms;

namespace WindowsFormsApp7
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void evt_DragDrop(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                var filePaths = (string[])(e.Data.GetData(DataFormats.FileDrop));
                textBox1.Text+= string.Join("\r\n", filePaths);

                foreach (var f in filePaths)
                    File.Copy(f, @"F:\Temp\" + Path.GetFileName(f),true);
            }
        }

        private void evt_DragEnter(object sender, DragEventArgs e)
        {
            e.Effect = DragDropEffects.All;
        }
    }
}

你的邮件系统是不是用的OutLook
如果是,可以用下面的文档。

Private Declare Sub Sleep Lib "kernel32.dll" (ByVal dwMilliseconds As Long)

Public Sub SaveAttach(MyItem As Outlook.MailItem)
    SaveAttachment MyItem, "C:\Data\MailAttached\"
    'MsgBox "附件已保存"
End Sub

Private Sub SaveAttachment(ByVal Item As Outlook.MailItem, path, Optional condition = "*")
    Dim olAtt As Outlook.Attachment
    Dim i As Integer
    Dim dateFormat
    dateFormat = Format(Now, "yyyy-mm-dd hh-mm-ss")
    If Item.Attachments.Count > 0 Then
        For i = 1 To Item.Attachments.Count
            Set olAtt = Item.Attachments(i)
            If olAtt.FileName Like condition Then
                olAtt.SaveAsFile path & dateFormat & "_" & olAtt.FileName
            End If
        Next
    End If
    Set olAtt = Nothing
    Sleep 1000
End Sub

相当于是文件拖拽没有获取到文件路径的问题?
如果是的话,可以试下看看对不对

Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    Me.AllowDrop = True
End Sub

Private Sub Form1_DragDrop(sender As System.Object, e As System.Windows.Forms.DragEventArgs) Handles Me.DragDrop
    Dim files() As String = e.Data.GetData(DataFormats.FileDrop)
    For Each path In files
        MsgBox(path)
    Next
End Sub

Private Sub Form1_DragEnter(sender As System.Object, e As System.Windows.Forms.DragEventArgs) Handles Me.DragEnter
    If e.Data.GetDataPresent(DataFormats.FileDrop) Then
        e.Effect = DragDropEffects.Copy
    End If
End Sub

估计是邮件系统和操作系统文件没有兼容,做不到直接拖拽。需要打通这个格式转换,才能实现直接的拖拽。你这边接收先要识别邮件系统中附件,再完成到文件的转换,但是这个不一定能成功,取决于拖拽和放置方法是否支持非文件格式。

从你的代码看, 看看fs是否有值

题主举个具体例子吧,楼上几位已经是获得文件路径后复制文件即可。
看看你获取的文件是什么和实际拖放到桌面的文件是否一样?
两个文件放出来看看。

插个眼

拖个WebBrowser控件,允许拖放,将Url设为指定目录,完事

img

VB也能这样操作,学习了

学习