把它当做文本处理读取里面的内容,再查找,匹配就好了。
可以参考这儿:http://www.cnblogs.com/xiaoyao2011/archive/2011/09/29/2195197.html
没有 更改成功
try
{
int totalFile = 0;
if (this.txtHTML.Text.Trim() == "")
{
MessageBox.Show("请输入HTML文件路径!");
}
else
{
string dirPath = this.txtHTML.Text.Trim();
if (!dirPath.Substring(dirPath.Length - 1).Contains("\\"))
{
dirPath = dirPath + "\\";
}
DirectoryInfo dirInfo = new DirectoryInfo(dirPath);
FileInfo[] files = dirInfo.GetFiles(); ;
int i = 0;
foreach (FileInfo fileinfo in files)
{
if (fileinfo.Extension.Equals(".htm"))//遍历所有htm文件
{
totalFile = totalFile + 1;
WebRequest myWebRequest = WebRequest.Create(dirPath + fileinfo.Name);
WebResponse myWebResponse = myWebRequest.GetResponse();
Stream myStream = myWebResponse.GetResponseStream();
Encoding encode = System.Text.Encoding.GetEncoding("gb2312");
StreamReader myStreamReader = new StreamReader(myStream, encode);
string strhtml = myStreamReader.ReadToEnd();//获取HTML文档里面的值
//int num = strhtml.IndexOf("心包炎");
string str = string.Format("<a href='#'>{0}</a>", "心包炎");
string strNew = strhtml.Replace("心包炎",str);//html文档值里面有 心包炎 的替换成 str
myWebResponse.Close();
string stroutput = strhtml;
SaveFileDialog sfd = new SaveFileDialog();
sfd.Title = "保存";
sfd.Filter = "htm file(*.htm)|*.htm";
sfd.ShowDialog();
string path = sfd.FileName;
FileInfo file = new FileInfo(path);
if (file.Exists)
{
file.Delete();
}
FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write);
StreamWriter sw = new StreamWriter(fs, Encoding.UTF8);
sw.Write(strNew);
sw.Flush();
sw.Close();
fs.Close();
}
}
}
}
catch (Exception ee) { MessageBox.Show("操作失败:" + ee.Message); }