How to drag-and-drop an .msg file from MS Outlook?
-
Hi, In my WPF application I have a requirement where in user can drag-and-drop an email (.msg file) either from the file system OR MS Outlook. When user drags-and-drop the .msg file from the file system the file gets added. But when user drags-and-drop a .msg file from MS Outlook, it throws following exception Exception from HRESULT: 0x80040068 (DV_E_LINDEX I have implemented the below code & I encounter error on below line MemoryStream ms = (MemoryStream)e.Data.GetData("FileContents", true);
string[] fileNames = null;
if (e.Data.GetDataPresent(DataFormats.FileDrop, false))
{
fileNames = e.Data.GetData(DataFormats.FileDrop, true) as string[];
foreach (string fileName in fileNames)
{
FileInfo fileInfo = new FileInfo(fileName);
// Some logic goes here...
}
}
else if (e.Data.GetDataPresent("FileGroupDescriptor"))
{
Stream theStream = (Stream)e.Data.GetData("FileGroupDescriptor");
byte[] fileGroupDescriptor = new byte[512];
theStream.Read(fileGroupDescriptor, 0, 512);
StringBuilder fileName = new StringBuilder("");
// Get the file name of the attached file
for (int i = 76; fileGroupDescriptor[i] != 0; i++)
{
fileName.Append(Convert.ToChar(fileGroupDescriptor[i]));
}
theStream.Close();
string path = Path.GetTempPath();
string theFile = path + fileName.ToString();
// Define full path for temp file
// Get the raw file into memory
MemoryStream ms = (MemoryStream)e.Data.GetData("FileContents", true);
byte[] fileBytes = new byte[ms.Length];
ms.Position = 0;
ms.Read(fileBytes, 0, (int)ms.Length);
FileStream fs = new FileStream(theFile, FileMode.Create);
fs.Write(fileBytes, 0, (int)fileBytes.Length);
fs.Close();
// Get the FileInfo to use
FileInfo tempFile = new FileInfo(theFile);
// Verify the file was created
if (tempFile.Exists)
{
// Some Logic goes here
// Delete the temporary file once done with it
tempFile.Delete();
}
What might be the issue here? Kindly help.
Regards, Vipul Mehta