1

我正在尝试使用 memorystream(在 ASP.NET 4.0 中)创建一个 ICS 日历文件,如下所示:

using System.IO;
using System;

public MemoryStream GetMemoryStream()
{
    iCalendar iCal = new iCalendar();
    Event evt = iCal.Create<Event>();

    // Set information about the event
    evt.Start = new iCalDateTime(SomeStartTime);
    evt.End = new iCalDateTime(SomeEndTime);
    evt.Location = SomeLocation;
    evt.Description = SomeDescription;
    evt.Summary = SomeSummary;

    iCalendarSerializer serializer = new iCalendarSerializer(iCal);
    MemoryStream ms = new MemoryStream();
    serializer.Serialize(iCal, ms, Encoding.UTF8);
    return ms;
}

我想我错过了一些东西。任何人都可以帮我创建正确MemoryStream的对象吗?

注意:我需要该MemorySteam对象来创建一个System.Net.Mail.Attachmentfor System.Net.Mail.MailMessage

4

1 回答 1

3

据我所见,MemoryStream 正在由序列化程序处理。我通过将日历转换为字符串然后将字符串转换为 MemoryStream 来解决它

using System.IO;
using System;

public MemoryStream GetMemoryStream()
{
    iCalendar iCal = new iCalendar();
    Event evt = iCal.Create<Event>();

    // Set information about the event
    evt.Start = new iCalDateTime(SomeStartTime);
    evt.End = new iCalDateTime(SomeEndTime);
    evt.Location = SomeLocation;
    evt.Description = SomeDescription;
    evt.Summary = SomeSummary;

    iCalendarSerializer serializer = new iCalendarSerializer(iCal);
    MemoryStream ms = new MemoryStream();
    //serializer.Serialize(iCal, ms, Encoding.UTF8);
    var ics = serializer.SerializeToString(iCal);
    var bytes = System.Text.Encoding.UTF8.GetBytes(ics);
    ms.Write(bytes, 0, bytes.Length); 
    ms.Seek(0, SeekOrigin.Begin);
    return ms;
}
于 2012-11-26T22:18:11.537 回答