1

我正在尝试复制 Outlook 电子邮件并将其粘贴到表单上。每当我执行粘贴操作时,Outlook 邮件(MSG 文件)都应该保存到某个目录,并且我已经实现了一些代码,如下所示。

private void OutlookMailCopyPaste_KeyDown(object sender, KeyEventArgs e)
{
  if (e.Control && e.KeyCode == Keys.V)
  {
    if (Clipboard.ContainsData("FileGroupDescriptor"))
    {
      if (Clipboard.ContainsData("FileGroupDescriptorW"))
      {
        Stream fileStream = (Stream)Clipboard.GetData("FileGroupDescriptor");
        byte[] fileGroupDescriptor = new byte[fileStream.Length];
        fileStream.Read(fileGroupDescriptor, 0, fileGroupDescriptor.Length);
        fileStream.Close();
        System.Text.StringBuilder fileName = new System.Text.StringBuilder("");
        for (int i = 76; i < fileGroupDescriptor.Length; i++)
        {
          fileName.Append(Convert.ToChar(fileGroupDescriptor[i]));
        }

        //fileGroupDescriptor = Encoding.Convert(Encoding.GetEncoding("iso-8859-1"), Encoding.UTF8, fileGroupDescriptor);
        //string tempString = Encoding.UTF8.GetString(fileGroupDescriptor, 0, fileGroupDescriptor.Length);
        string tempString = fileName.ToString().Substring(fileName.ToString().IndexOf('.'));
        if (tempString.ToUpper().StartsWith(".MSG"))
        {
          if (Clipboard.ContainsData("FileContents"))
          {
            MemoryStream[] filestreams = (MemoryStream[])Clipboard.GetData("FileContents");
            if (filestreams != null && filestreams.Length > 0)
            {
              MemoryStream filestream = filestreams[0];
              //save the file stream using its name to the application path
              FileStream outputStream = File.Create(fileName.ToString());
              filestream.WriteTo(outputStream);
              outputStream.Close();
            }
          }
        }
        else
        {
          MessageBox.Show("Please copy and paste MSG file only.");
        }
      }
    }
  }
}

当我将剪贴板数据读入内存流时,我会遇到一个异常,例如“内存不足,无法继续执行程序”。在线

MemoryStream[] 文件流 = (MemoryStream[])Clipboard.GetData("FileContents");

此外 Clipboard.GetData("FileContents") 有 null 虽然我正在检查它是否在此行之前有一些数据

if (Clipboard.ContainsData("FileContents"))

谢谢和问候, 拉梅什·莫拉萨

4

0 回答 0