[英]how can I save HTML content of HTML editor in c#
我使用此代碼在c#中的HTML編輯器中打開HTML頁面。
private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
using (OpenFileDialog ofd = new OpenFileDialog() { Multiselect =
false, ValidateNames = true, Filter = "HTML|*.html" })
if (ofd.ShowDialog() == DialogResult.OK)
{
textBox1.Text = ofd.FileName;
FileStream fs = new FileStream(ofd.FileName, FileMode.Open, FileAccess.Read);
webBrowser1.DocumentStream = fs;
}
}
而且我也用這段代碼來保存更改
private void saveToolStripMenuItem_Click(object sender, EventArgs e)
{
SaveFileDialog svf = new SaveFileDialog();
svf.Filter = "Text Files (.html)|*.html";
if (svf.ShowDialog() == DialogResult.OK)
{
System.IO.StreamWriter sw = new System.IO.StreamWriter(svf.FileName);
sw.WriteLine(webBrowser1);
sw.Close();
}
}
但是,在我的HTML頁面中保存的唯一一行是以下消息:System.Windows.Forms.WebBrowser。 您是否知道如何保存HTML頁面的內容? 謝謝
以下應按您的要求工作:
System.IO.StreamWriter sw = new System.IO.StreamWriter(svf.FileName); webBrowser1.DocumentStream.CopyTo(sw.BaseStream); sw.Flush(); sw.Close();
您無法使用的原因是因為您試圖直接將對象寫入流,如所述,該對象隱式調用ToString()方法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.