如何以编程方式保存用户已在C#中使用CTRL-C复制到剪贴板的Outlook电子邮件附件例如PDF

问题描述

上下文:

当复制到剪贴板的文件例如是桌面上的文件时,我能够从剪贴板复制(并使用CTRL-C)并以编程方式粘贴文件。使用以下语法,这很简单:

File.Copy(Clipboard.GetFileDropList()[0],savePath)

其中Clipboard.GetFileDropList()[0]返回复制文件的路径,而savePath是粘贴位置。

但是,我发现如果复制的文件(使用CTRL-C)是Outlook电子邮件中的文件附件,则上述语法不起作用。在这种情况下,Clipboard.ContainsFileDropList()返回false,而Clipboard.GetFileDropList()[0]导致以下错误消息:

“ ArgumentOutOfRangeException:索引超出范围。必须为非负数,并且小于集合的大小。参数名称:index”

尽管事实是按CTRL-V确实成功粘贴了文件,并确认文件最初已成功复制到剪贴板。

问题:

对不起,如果我错过了一些很基本的东西。我的问题是,当从Outlook内部使用CTRL-C将电子邮件附件复制到剪贴板时,如何以编程方式将剪贴板中的电子邮件附件(PDF,Word等)粘贴/保存到文件位置。

请注意,我确实了解,可以通过跳过剪贴板并与Outlook进行编程交互以访问选定的电子邮件附件来解决我要执行的操作。但是,我的目标是学习在不同情况下如何与剪贴板进行编程交互。

解决方法

您使用了错误的DataFormat。您始终可以通过调用Clipboard.GetDataObject().GetFormats()来获取当前存在的数据格式的列表。

您需要使用:

"FileGroupDescriptor"检索文件名

private static async Task<List<string>> GetAttachedFileNamesFromClipboardAsync(IDataObject clipboardData)
{
  if (!clipboardData.GetDataPresent("FileGroupDescriptor"))
  {
    return new List<string>();
  }

  using (var descriptorStream = clipboardData.GetData("FileGroupDescriptor",true) as MemoryStream)
  {
    using (var streamReader = new StreamReader(descriptorStream))
    {
      var streamContent = await streamReader.ReadToEndAsync();
      string[] fileNames = streamContent.Split(new[] { '\0' },StringSplitOptions.RemoveEmptyEntries);
      return new List<string>(fileNames.Skip(1));
    }
  }
}

"FileContents"检索原始文件内容

// Returns the attachment file content as string
private static async Task<string> GetAttachmentFromClipboardAsync(IDataObject clipboardData)
{
  if (!clipboardData.GetDataPresent("FileContents"))
  {
    return string.Empty;
  }

  using (var fileContentStream = clipboardData.GetData("FileContents",true) as MemoryStream)
  {
    using (var streamReader = new StreamReader(fileContentStream))
    {
      return await streamReader.ReadToEndAsync();
    }
  }
}

// Returns the attachment file content as MemoryStream
private static MemoryStream GetAttachmentFromClipboard(IDataObject clipboardData)
{
  if (!clipboardData.GetDataPresent("FileContents"))
  {
    return null;
  }

  return clipboardData.GetData("FileContents",true) as MemoryStream;
}

将附件保存到磁盘

由于Windows仅将第一个选定的附件添加到系统剪贴板,因此该解决方案只能保存单个附件。显然办公室剪贴板不可访问。

private static async Task SaveAttachmentFromClipboardToFileAsync(IDataObject clipboardData,string destinationFilePath)
{
  if (!clipboardData.GetDataPresent("FileContents"))
  {
    return;
  }

  using (var attachedFileStream = clipboardData.GetData("FileContents",true) as MemoryStream)
  {
    using (var destinationFileStream = File.Open(destinationFilePath,FileMode.OpenOrCreate))
    {
      await attachedFileStream.CopyToAsync(destinationFileStream);
    }
  }
}

使用Office API将附件保存到磁盘上

需要引用 Microsoft.Office.Interop.Outlook.dll 。此解决方案不依赖于系统剪贴板。它只是从Outlook资源管理器中当前打开的邮件项中提取选定的附件。
您仍然可以监视系统剪贴板,以触发保存附件的过程。

private static void SaveSelectedAttachementsToFolder(string destinationFolderPath)
{
  var outlookApplication = new Microsoft.Office.Interop.Outlook.Application();
  Explorer activeOutlookExplorer = outlookApplication.ActiveExplorer();
  AttachmentSelection selectedAttachments = activeOutlookExplorer.AttachmentSelection;

  foreach(Attachment attachment in selectedAttachments)
  {
    attachment.SaveAsFile(Path.Combine(destinationFolderPath,attachment.FileName));
  }
}

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...