views:

703

answers:

2

I'm trying to allow an attachment from an email open in Novell GroupWise to be dropped into my C# WinForms application. The standard .NET functionality doesn't work.

In the DragDrop event of a control, e.Data.GetFormats() returns the following.

FileGroupDescriptorW
FileGroupDescriptor
FileContents
attachment format

I can get the filename with e.Data.GetData("FileGroupDescriptor") and going to position 76.

Unfortunately, e.Data.GetData("FileContents") causes a first chance System.NotImplementedException in System.Windows.Forms.dll and returns null. Attachment format also returns null.

My searches tell me that drag and drop is a lot more complex than I thought :) It seems like GroupWise might be using a format called CFSTR_FILECONTENTS but that's just a guess. The attachments can be successfully dragged and dropped onto the Windows desktop or other folders.

Thanks for any suggestions.

+2  A: 

I had no luck finding this too. Here is what I came up with (Groupwise 7):

private void control_DragDrop(object sender, DragEventArgs e)
{
   string strFilename = null;

   //something about the act of reading this stream creates the file in your temp folder(?)
   using (MemoryStream stream = (MemoryStream)e.Data.GetData("attachment format", true))
   {
       byte[] b = new byte[stream.Length];
       stream.Read(b, 0, (int)stream.Length);
       strFilename = Encoding.Unicode.GetString(b);
       //The path/filename is at position 10.
       strFilename = strFilename.Substring(10, strFilename.IndexOf('\0', 10) - 10);
       stream.Close();
   }

   if (strFilename != null && File.Exists(strFilename))
   {
      //From here on out, you're just reading another file from the disk...
      using(FileStream fileIn = File.Open(strFilename, FileMode.Open))
      {
          //Do your thing
          fileIn.Close();
      }
   }

   File.Delete(strFilename);
}
Wow, thanks John. That's very cool and also kind of bizarre :-) You're right, simply accessing e.Data.GetData("attachment format") (not sure if it even needs the ,true) creates the file. Somehow I thought that was returning null before but I must have been confused. Thanks again.CheersRoss
tetranz
A: 

Man this helped me a bunch and I am so close to getting it but I can't seem to read what is in the b byte array. The resulting strFileName displays like a square or unknown symbol...no string is in there, but when I write the array to a file, it has the path and file name I need. What am I doing wrong?

oMemoryStream = e.Data.GetData("attachment format", True) Dim bFileContents(oMemoryStream.Length) As Byte oMemoryStream.Read(bFileContents, 0, oMemoryStream.Length) strFileName = Encoding.Unicode.GetString(bFileContents) strFileName = strFileName.Substring(10, strFileName.IndexOf("\0", 10) - 10) oMemoryStream.Close()

Peter