CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    May 2007
    Location
    Denmark
    Posts
    623

    Memory stream from DragDrop operation

    Hi guys,

    I'm working on an application that uses a DragDrop function to allow the user to drop files onto my form. This works fine when I'm working with normal files, documents and such. But I need it to work when the user drags an e-mail from Outlook onto the form too.

    So far I've tried using the FileDrop format for this, which didn't work. I then found that the data has about 10-13 formats when I use the IDataObject.GetFormats method. It seems I need to somehow read some data from a memorystream which is contained in the IDataObject.
    I hope this is understandable.

    What I need is the path (local or UNC) to the .msg file that the user is dropping on my form. This is my current code:

    Code:
    //IDataObject data is taken in as a parameter
    string[] formats = data.GetFormats(true);
    bool done = false;
    for (int i = 0; i < formats.Length && !done; i++)
    {
        try
        {
            object o = data.GetData(formats[i]);
            if (o.GetType() == typeof(System.IO.MemoryStream))
            {
                byte[] buffer = new byte[((MemoryStream)o).Length];
    
                ((MemoryStream)o).Read(buffer, 0, buffer.Length);
                path = Encoding.UTF8.GetString(buffer);
                if (!File.Exists(path))
                    path = null;
            }
            if (path != null)
                done = true;
        }
        catch { }
    }
    When I debug I can see that some of the objects I get out contains the details of the .msg file, but none contain the path.
    The byte[] mostly contains all zeros, but some of them seem to actually contain data (not surprising), but I don't know how to read it.

    I know this is probably a poor attempt, but I had to start somewhere Any help will be greatly appreciated
    It's not a bug, it's a feature!

  2. #2
    Join Date
    May 2009
    Location
    Bengaluru, India
    Posts
    460

    Re: Memory stream from DragDrop operation

    I will try to answer with some knowledge i have :

    first of all the usage : IDataObject data : interface seems to be related to COM and since the things you are dragging and dropping are from OUTLOOK side the com server side implementation would be done on its side.
    so you never know that the com server supports the feature which you are trying to work on client side ( i.e, c# side ) , thats why you might be receiving zero data !!

    one more doubt i have at the end you mention abt dont know how to read - but your function code has that part of reading and converting from bytes to string.. ??
    Last edited by vcdebugger; July 17th, 2009 at 06:50 AM.

  3. #3
    Join Date
    May 2007
    Location
    Denmark
    Posts
    623

    Re: Memory stream from DragDrop operation

    I haven't been keeping this thread up to date, but I have gotten a little further. I can now, thanks to this codeproject article, read some information about the .msg file. I can also create an .msg file with the correct contents.

    I'm now facing another problem. Whenever I drag a message onto my form wherein the sender address in is my domain, I don't get the sender e-mail address, but rather a string that somewhat resembles an X.400 string. Does anyone know how I might change this? This is the code that gets the info:

    Code:
    using Outlook = Microsoft.Office.Interop.Outlook;
    
    Outlook.Application app = new Microsoft.Office.Interop.Outlook.Application();
    Outlook._NameSpace nameSpace = app.GetNamespace("MAPI");
    nameSpace.Logon(null, null, false, false);
    Outlook.Folder folder = (Outlook.Folder)app.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderDrafts);
    Outlook.MailItem msg = (Outlook.MailItem)app.CreateItemFromTemplate(filename, folder);
    string sender = msg.SenderEmailAddress;
    string[] recipients = new string[msg.Recipients.Count];
    for (int i = 0; i < recipients.Length; i++)
        recipients[i] = msg.Recipients[i].Address;
    It's not a bug, it's a feature!

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured