Drag & Drop Mail

sabato 16 febbraio 2008 - 13.47

SSUPERPIPPO Profilo | Guru

Ciao a tutti

Stò implementando la funzionalità di drag & drop di files e mail provenienti da Outlook (non express)

Il codice che ho utilizzato è il seguente:

Private Sub LstAllegati_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles LstAllegati.DragEnter

If (e.Data.GetDataPresent(DataFormats.FileDrop)) Then
e.Effect = DragDropEffects.Copy
ElseIf e.Data.GetDataPresent("FileGroupDescriptor") Then
e.Effect = DragDropEffects.Copy
Else
e.Effect = DragDropEffects.None
End If

End Sub

' E fin qui tutto bene

Private Sub LstAllegati_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles LstAllegati.DragDrop

Dim fileNames() As String = Nothing

Try
If (e.Data.GetDataPresent(DataFormats.FileDrop, False) = True) Then
fileNames = CType(e.Data.GetData(DataFormats.FileDrop), String())

' Cicla i files oggetto del trascinamento
For Each fileName As String In fileNames
CaricaFile(fileName)
Next
ElseIf e.Data.GetDataPresent("FileGroupDescriptor") Then

' 1. Ottiene FileGroupDescriptor e ne estrae il nome file

Dim theStream As Stream = CType(e.Data.GetData("FileGroupDescriptor"), Stream)
Dim fileGroupDescriptor() As Byte = New Byte((512) - 1) {}

theStream.Read(fileGroupDescriptor, 0, 512)

' recupera il nome file

Dim fileName As StringBuilder = New StringBuilder("")

Dim i As Integer = 76
Do While (fileGroupDescriptor(i) <> 0)
fileName.Append(Convert.ToChar(fileGroupDescriptor(i)))
i = (i + 1)
Loop

theStream.Close()

Dim path As String = Application.StartupPath

' Crea il percorso completo per il file nella directory di programma (temporaneamente)
Dim theFile As String = (path + "\" + fileName.ToString)

' 2. Recuperare e il file dalla memoria

'
Dim ms As MemoryStream = CType(e.Data.GetData("FileContents", True), MemoryStream)

E QUI' CHE SI PRESENTA IL PROBLEMA: MS RIMANE A NOTHING

Dim fileBytes() As Byte = New Byte((ms.Length) - 1) {}
ms.Position = 0
ms.Read(fileBytes, 0, CType(ms.Length, Integer))

' Crea il file e salva il raw zip file su di esso
Dim fs As FileStream = New FileStream(theFile, FileMode.Create)
fs.Write(fileBytes, 0, CType(fileBytes.Length, Integer))
fs.Close()

Dim tempFile As FileInfo = New FileInfo(theFile)
If (tempFile.Exists = True) Then
tempFile.Delete()
Else
Trace.WriteLine("Impossibile creare il file!")
End If
End If
Catch ex As Exception
Trace.WriteLine(("Errore nella funzione di Drag & Drop: " + ex.Message))
End Try

End Sub

L'istruzione

Dim ms As MemoryStream = CType(e.Data.GetData("FileContents", True), MemoryStream)

che dovrebbe copiare in ms il file vero e proprio della mail, contiene sempre Nothing

Dove sbaglio????

Grazie 1000

http://blogs.dotnethell.it/alebadalin
Partecipa anche tu! Registrati!
Hai bisogno di aiuto ?
Perchè non ti registri subito?

Dopo esserti registrato potrai chiedere
aiuto sul nostro Forum oppure aiutare gli altri

Consulta le Stanze disponibili.

Registrati ora !
Copyright © dotNetHell.it 2002-2024
Running on Windows Server 2008 R2 Standard, SQL Server 2012 & ASP.NET 3.5