Support
Joined: 18 Jul 2005 Posts: 731
|
Posted: Fri Jan 16, 2009 11:14 am Post subject: HOWTO: Access stream data from data object during drag-drop. |
|
|
Some data sources such as Outlook and Outlook Express provide data (emails/attachments) via streams. When emails from Outlook/Outlook Express (or another source which provides data via streams) are dragged to your namespace extension, use the following code to access that data in the NSEItem.DragDrop event:
[C#]
Code: | // Called when a drop occurs over the item.
public override void DragDrop(NSEDragEventArgs e)
{
FileDescriptor[] descriptors = e.Data.GetFileDescriptors();
if (descriptors != null && descriptors.Length > 0)
{
// Data available as streams, enumerate
for (int i = 0; i < descriptors.Length; i++)
{
Stream s = null;
// Get stream number 'i'
object temp = e.Data.GetFileContent(i);
if (temp == null)
continue;
// The stream can be returned via one of the following ways..
if (temp is IntPtr) // via IStorage
{
s = Utils.GetStreamFromStorage((IntPtr)temp);
}
else if (temp is Stream) // via stream
{
s = temp as Stream;
}
else if (temp is HandleRef) // via HGLOBAL
{
s = Utils.GetStreamFromHGLOBAL((HandleRef)temp);
}
// Create file where stream data will be saved
FileStream fs = File.Create(Path.Combine(fullPath, descriptors[i].FileName));
// Copy from stream to file
byte[] buffer = new byte[1024];
int readBytes;
while ((readBytes = s.Read(buffer, 0, buffer.Length)) != 0)
{
fs.Write(buffer, 0, readBytes);
}
s.Close();
fs.Close();
}
e.Data.PerformedDropEffect = e.Effect;
if (e.Data.PreferredDropEffect == DragDropEffects.Move)
e.Data.PasteSucceded = e.Effect;
this.RefreshView();
}
} |
[VB.Net]
Code: | ' Called when a drop occurs over the item.
Public Overrides Sub DragDrop(ByVal e As NSEDragEventArgs)
Dim descriptors As FileDescriptor() = e.Data.GetFileDescriptors()
If (descriptors Is Nothing = False And descriptors.Length > 0) Then
' Data available as streams, enumerate
Dim i As Integer
For i = 0 To descriptors.Length - 1
Dim s As Stream = Nothing
' Get stream number 'i'
Dim temp As Object = e.Data.GetFileContent(i)
If (temp Is Nothing = False) Then
System.Diagnostics.Debug.WriteLine("Data")
' The stream can be returned via one of the following ways..
If (TypeOf (temp) Is IntPtr) Then ' via IStorage
s = Utils.GetStreamFromStorage(CType(temp, IntPtr))
System.Diagnostics.Debug.WriteLine("IntPtr")
ElseIf (TypeOf (temp) Is Stream) Then ' via stream
s = CType(temp, Stream)
System.Diagnostics.Debug.WriteLine("Stream")
ElseIf (TypeOf (temp) Is HandleRef) Then ' via HGLOBAL
s = Utils.GetStreamFromHGLOBAL(CType(temp, HandleRef))
System.Diagnostics.Debug.WriteLine("HandleRef")
End If
' Create file where stream data will be saved
Dim fs As FileStream = File.Create(Path.Combine(fullPath, descriptors(i).FileName))
' Copy from stream to file
Dim buffer As Byte() = New Byte(1024) {}
Dim readBytes As Integer
While (True)
readBytes = s.Read(buffer, 0, buffer.Length)
If readBytes = 0 Then
Exit While
End If
fs.Write(buffer, 0, readBytes)
End While
System.Diagnostics.Debug.WriteLine(buffer)
s.Close()
fs.Close()
End If
Next i
e.Data.PerformedDropEffect = e.Effect
If (e.Data.PreferredDropEffect = DragDropEffects.Move) Then
e.Data.PasteSucceded = e.Effect
End If
Me.RefreshView()
End If
End Sub
|
|
|