从Outlook拖拽附件到WinForm

当从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[i]!=0; i++)
{ fileName.Append(Convert.ToChar(fileGroupDescriptor[i]));}
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



返回导读目录,阅读更多随笔



分割线,以下为博客签名:

软件臭虫情未了
  • 编码一分钟
  • 测试十年功


随笔如有错误或不恰当之处、为希望不误导他人,望大侠们给予批评指正。

原文地址:https://www.cnblogs.com/08shiyan/p/2113227.html