设为首页 收藏本站
查看: 1271|回复: 0

[经验分享] 从Outlook拖拽附件到WinForm

[复制链接]

尚未签到

发表于 2015-9-13 08:40:13 | 显示全部楼层 |阅读模式
当从OutLook中拖拽附件时,其拖拽的源共有四种DataFormat,其中FileGroupDescriptor 包含拖拽的文件名,FileContents中是文件内容。
当然Copy附件也是一样。



如下代码演示了一个简单的拖拽,并将拖拽的附件保存到了系统临时目录下。




//
// Program created by Thomas (Tom) F. Gueth, Binary Star Technology, Inc
//
// You are welcome to use this sample code in any manner you wish, commercial or otherwise.
//
// No warranty of any kind is made about this code.  It worked for me and I hope it works for you
//   and expands your knowledge of drag and drop technology.
//

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Windows.Forms;
namespace TestEmailDragDrop
{
    /// <summary>
    /// Summary description for Form1.
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.Container components = null;
        public Form1()
        {
            //
            // Required for Windows Form Designer support
            //
            InitializeComponent();
            //
            // TODO: Add any constructor code after InitializeComponent call
            //
        }
        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                if (components != null)
                {
                    components.Dispose();
                }
            }
            base.Dispose( disposing );
        }
        #region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            //
            // Form1
            //
            this.AllowDrop = true;
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(456, 341);
            this.Name = "Form1";
            this.Text = "Drag-and-Drop Email Attachment";
            this.DragDrop += new System.Windows.Forms.DragEventHandler(this.Form1_DragDrop);
            this.DragEnter += new System.Windows.Forms.DragEventHandler(this.Form1_DragEnter);
        }
        #endregion
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.Run(new Form1());
        }
        private void Form1_DragEnter(object sender, System.Windows.Forms.DragEventArgs e)
        {
            // for this program, we allow a file to be dropped from Explorer
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {    e.Effect = DragDropEffects.Copy;}
            //    or this tells us if it is an Outlook attachment drop
            else if (e.Data.GetDataPresent("FileGroupDescriptor"))
            {    e.Effect = DragDropEffects.Copy;}
            //    or none of the above
            else
            {    e.Effect = DragDropEffects.None;}
        }
        private void Form1_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
        {
            string [] fileNames = null;
            try
            {
                if ( e.Data.GetDataPresent(DataFormats.FileDrop,false) == true)
                {                    
                    fileNames = (string []) e.Data.GetData(DataFormats.FileDrop);
                    // handle each file passed as needed
                    foreach( string fileName in fileNames)
                    {
                        // do what you are going to do with each filename
                    }
                }
                else if (e.Data.GetDataPresent("FileGroupDescriptor"))
                {
                    //
                    // the first step here is to get the filename of the attachment and
                    //   build a full-path name so we can store it in the temporary folder
                    //
                    // set up to obtain the FileGroupDescriptor and extract the file name
                    Stream theStream = (Stream) e.Data.GetData("FileGroupDescriptor");
                    byte [] fileGroupDescriptor = new byte[512];
                    theStream.Read(fileGroupDescriptor,0,512);
                    // used to build the filename from the FileGroupDescriptor block
                    StringBuilder fileName = new StringBuilder("");
                    // this trick gets the filename of the passed attached file
                    for(int i=76; fileGroupDescriptor!=0; i++)
                    {    fileName.Append(Convert.ToChar(fileGroupDescriptor));}
                    theStream.Close();
                    string path = Path.GetTempPath();  // put the zip file into the temp directory
                    string theFile = path+fileName.ToString();  // create the full-path name
                    //
                    // Second step:  we have the file name.  Now we need to get the actual raw
                    //    data for the attached file and copy it to disk so we work on it.
                    //
                    // get the actual raw file into memory
                    MemoryStream ms = (MemoryStream) e.Data.GetData("FileContents",true);
                    // allocate enough bytes to hold the raw data
                    byte [] fileBytes = new byte[ms.Length];
                    // set starting position at first byte and read in the raw data
                    ms.Position = 0;
                    ms.Read(fileBytes,0,(int)ms.Length);
                    // create a file and save the raw zip file to it
                    FileStream fs = new FileStream(theFile,FileMode.Create);
                    fs.Write(fileBytes,0,(int)fileBytes.Length);
                    fs.Close();    // close the file

                    FileInfo tempFile = new FileInfo(theFile);
                    // always good to make sure we actually created the file
                    if ( tempFile.Exists == true)
                    {   
                        // for now, just delete what we created
                        tempFile.Delete();
                    }
                    else
                    {    Trace.WriteLine("File was not created!");}
                }
            }
            catch (Exception ex)
            {
                Trace.WriteLine("Error in DragDrop function: " + ex.Message);
                // don't use MessageBox here - Outlook or Explorer is waiting !
            }
        }
    }
}
  
  代码源于:http://www.codeproject.com/KB/cs/testemaildragdrop.aspx

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.iyunv.com/thread-112857-1-1.html 上篇帖子: Outlook Express 收件箱容量超过2G时的处理办法 下篇帖子: outlook 收Gmail邮箱邮件
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表