0

我一直在编写一个 C# 程序来从电子邮件中检索 XML 附件并将它们保存到磁盘以供进一步处理。

if (message.Attachments != null && message.Attachments.Any())
{
    foreach (var attachment in message.Attachments)
    {
        attachment.Download();
        var fileName = String.Format("{0}-{2}{1}", Path.GetFileNameWithoutExtension(attachment.FileName), Path.GetExtension(attachment.FileName), Guid.NewGuid());
        attachment.Save(attachmentsPath, fileName);
        string contents = File.ReadAllText(attachmentsPath + "\\" + fileName);
        MessageBox.Show(String.Format("Attachment saved: {0}", fileName));
    }
}

一些 XML 无效,因为它们以 SUB 字符 (ASCII 26) 结尾,此时 C# 弹出错误:

attachment.Save(attachmentsPath, fileName);

mscorlib.dll 中出现“System.FormatException”类型的未处理异常输入不是有效的 Base-64 字符串,因为它包含非 base 64 字符、两个以上的填充字符或填充字符中的非法字符。

在将 XML 保存到磁盘或使其忽略此特殊字符之前,如何从该字符中删除 XML 附件?

4

0 回答 0