电子邮件附件拖放到C盘

时间:2015-07-13 09:02:04

标签: vb.net drag-and-drop outlook

我正在尝试向我的表单添加拖放功能,以便我可以从电子邮件中拖动附件,它将保存附件。

我收到一条错误消息,指出客户未持有所需的权限。我想解决这个问题。

这是我的代码:

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

Dim theStream As Stream = DirectCast(e.Data.GetData("FileGroupDescriptor"), Stream)
Dim fileGroupDescriptor As Byte() = New Byte(511) {}
theStream.Read(fileGroupDescriptor, 0, 512)

Dim fileName As New StringBuilder("")
Dim i As Integer = 76
While fileGroupDescriptor(i) <> 0
    fileName.Append(Convert.ToChar(fileGroupDescriptor(i)))
    i += 1
End While
theStream.Close()

Dim theFile As String = "c:\" + fileName.ToString() 'change the c:\ to any path you want

Dim ms As MemoryStream = DirectCast(e.Data.GetData("FileContents", True), MemoryStream)
Dim fileBytes As Byte() = New Byte(ms.Length - 1) {}
ms.Position = 0
ms.Read(fileBytes, 0, CInt(ms.Length))

Dim fs As New FileStream(theFile, FileMode.OpenOrCreate)
fs.Write(fileBytes, 0, CInt(fileBytes.Length))
fs.Close()
End If

1 个答案:

答案 0 :(得分:1)

您肯定无权写信c:\

我建议使用My.Computer.FileSystem.SpecialDirectories对象写入您的桌面或某处: -

   Dim theFile As String = IO.Path.Combine(My.Computer.FileSystem.SpecialDirectories.Desktop, filename.ToString)

查看此文章,了解您可以使用的所有选项:https://msdn.microsoft.com/en-us/library/microsoft.visualbasic.fileio.specialdirectories.aspx