的無法打開文件,我得到了下面的代碼的StreamWriter可以因爲一個進程
protected override void Render(HtmlTextWriter writer)
{
// Write the HTML into this string builder
StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);
HtmlTextWriter hWriter = new HtmlTextWriter(sw);
base.Render(hWriter);
string pageHTML = sb.ToString();
// Write it back to the server
writer.Write(pageHTML);
if (Convert.ToBoolean(this.ViewState["SendEmail"]))
{
string HTML = "";
HTML = "<!DOCTYPE HTML PUBLIC '-//IETF//DTD HTML//EN'>";
HTML += "<html>";
HTML += "<head>";
HTML += "<meta http-equiv='Content-Type'";
HTML += "content='text/html; charset=iso-8859-1'>";
HTML += "<title>Order Information</title>";
HTML += "</head>";
HTML += "<body>";
HTML += "See attachment for information.";
HTML += "</body>";
HTML += "</html>";
MailMessage mail = new MailMessage("[email protected]", "[email protected]", "Subject", HTML);
mail.IsBodyHtml = true;
string path = @"d:\websites\plate.html";
using (StreamWriter sw11 = File.CreateText(path))
{
sw11.WriteLine(pageHTML);
}
mail.Attachments.Add(new Attachment(path));
SmtpClient client = new SmtpClient("192.168.1.127");
client.Send(mail);
Response.Write("<script>alert('Your information has been sent.')</script>");
this.ViewState["SendEmail"] = false;
}
}
清新感/編譯我的解決方案的,當我按下發送鍵,調用該函數和HTML頁面後通過郵件附件發送沒有問題。但是,如果我嘗試再次按下發送按鈕,則會收到「System.IO.IOException:進程無法訪問文件'd:\ websites \ plate.html',因爲它正在被另一個進程使用。」當我試圖打開文件時發生錯誤。怎麼了?
我希望硬編碼路徑只是爲了簡潔起見。在生產中,如果(如果)兩個或更多用戶同時執行此操作,則會出現問題。 –
是的,例如,這是一個硬編碼路徑。 – Shadowizoo