gpt4 book ai didi

c# - File.WriteAllLines 似乎没有关闭

转载 作者:行者123 更新时间:2023-11-30 20:42:12 30 4
gpt4 key购买 nike

我正在尝试向网站添加一项功能,该功能会将 outlook 邀请附件发送到电子邮件。一切都在第一次完美运行,但再次运行后,我收到以下错误:“该进程无法访问文件‘OutlookCalendar.ics’,因为它正被另一个进程使用。”我已经到处寻找答案,但似乎当我使用 file.WriteAllLines 时,它不会关闭文件?请参阅下面的代码...

     //CREATE OUTLOOK ATTACHMENT.....................................................................................................
//..............................................................................................................................
string schLocation = "Kettle Cuisine TBD";
string schSubject = visitor + " is visiting.";
string schDescription = other;
System.DateTime schBeginDate = Convert.ToDateTime(date + " " + time);
System.DateTime schEndDate = Convert.ToDateTime(date + " 17:00");

//PUTTING THE MEETING DETAILS INTO AN ARRAY OF STRING

String[] contents = { "BEGIN:VCALENDAR",
"PRODID:-//Flo Inc.//FloSoft//EN",
"BEGIN:VEVENT",
"DTSTART:" + schBeginDate.ToUniversalTime().ToString("yyyyMMdd\\THHmmss\\Z"),
"DTEND:" + schEndDate.ToUniversalTime().ToString("yyyyMMdd\\THHmmss\\Z"),
"LOCATION:" + schLocation,
"DESCRIPTION;ENCODING=QUOTED-PRINTABLE:" + schDescription,
"SUMMARY:" + schSubject, "PRIORITY:3",
"END:VEVENT", "END:VCALENDAR" };

/*THE METHOD 'WriteAllLines' CREATES A FILE IN THE SPECIFIED PATH WITH
THE SPECIFIED NAME,WRITES THE ARRAY OF CONTENTS INTO THE FILE AND CLOSES THE
FILE.SUPPOSE THE FILE ALREADY EXISTS IN THE SPECIFIED LOCATION,THE CONTENTS
IN THE FILE ARE OVERWRITTEN*/

System.IO.File.WriteAllLines(Server.MapPath("OutlookCalendar.ics"), contents);

//END CREATE OUTLOOK ATTACHMENT.....................................................................................................
//..............................................................................................................................


//Send out email
Email.sendEmailOutlook(message, email, header);

在电子邮件部分我使用这个.... //从创建的 .ICS 文件中创建附件

        Attachment mailAttachment = new Attachment(System.Web.HttpContext.Current.Server.MapPath("OutlookCalendar.ics"));
mail.Attachments.Add(mailAttachment);

mail.IsBodyHtml = true;
//Set email message and subjet
mail.Subject = header;
mail.Body = message;

smtpClient.Send(mail);

最佳答案

我怀疑问题不在于 File.WriteAllLines,而在于 Attachment - 实现了 IDisposable,但你没有处理它的......所以如果它有一个打开的文件句柄,那将解释你所看到的。你可以只使用:

var path = HttpContext.Current.Server.MapPath("OutlookCalendar.ics");
using (Attachment mailAttachment = new Attachment(path))
{
mail.Attachments.Add(mailAttachment);

mail.IsBodyHtml = true;
//Set email message and subjet
mail.Subject = header;
mail.Body = message;

smtpClient.Send(mail);
}

但是,根本不清楚为什么需要写入文件。最好将您的代码更改为仅创建一个 MemoryStream。例如:

string text = string.Join("\r\n", contents);
byte[] encoded = Encoding.UTF8.GetBytes(text);
var stream = new MemoryStream(encoded);
using (var attachment = new Attachment(stream, "OutlookCalendar.ics"))
{
...
}

现在根本不涉及任何文件,所以不会有任何冲突。

关于c# - File.WriteAllLines 似乎没有关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31571820/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com