我有一个 System.Net.Mail.Attachment 对象,其中包含一些 .csv 数据。我需要将附件的内容保存在一个文件中。我试过这个:
var sb = new StringBuilder();
sb.AppendLine("Accounts,JOB,Usage Count");
sb.AppendLine("One,Two,Three");
sb.AppendLine("One,Two,Three");
sb.AppendLine("One,Two,Three");
var stream = new MemoryStream(Encoding.ASCII.GetBytes(sb.ToString()));
//Add a new attachment to the E-mail message, using the correct MIME type
var attachment = new Attachment(stream, new ContentType("text/csv"))
{
Name = "theAttachment.csv"
};
var sr = new StreamWriter(@"C:\Blah\Look.csv");
sr.WriteLine(attachment.ContentStream.ToString());
sr.Close();
但该文件只有以下内容:“System.IO.MemoryStream”。你能告诉我如何在那里获得真实数据吗?
谢谢。