日期:2014-05-17 浏览次数:20623 次
try { int totalFile = 0; if (this.textBox1.Text.Trim() == "") { MessageBox.Show("请输入HTML文件路径!"); } else if (comboBox1.Text == "请选择") { MessageBox.Show("请选择文件类型!"); } else { string dirPath = this.textBox1.Text.Trim(); if (!dirPath.Substring(dirPath.Length - 1).Contains("\\")) { dirPath = dirPath + "\\"; } StreamWriter sw = null; ; DirectoryInfo dirInfo = new DirectoryInfo(dirPath); FileInfo[] files = dirInfo.GetFiles(); foreach (FileInfo fileinfo in files) { if (fileinfo.Extension.Equals(comboBox1.Text))//遍历所有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(); myWebResponse.Close(); string stroutput = strhtml; //替换 stroutput = stroutput.Replace(textBox2.Text.Trim(), textBox4.Text.Trim()); string fileName = fileinfo.ToString(); //创建文件夹 if (!Directory.Exists(dirPath + "/NewFile")) Directory.CreateDirectory(dirPath + "/NewFile"); //生成静态文件 try { sw = new StreamWriter(dirPath + "NewFile/" + fileName, false, Encoding.GetEncoding("gb2312")); sw.Write(stroutput); sw.Flush(); } catch (Exception ex) { sw.Close(); throw ex; } finally { sw.Close(); } } label5.Text = textBox1.Text + "\\NewFile"; label7.Text = "共替换" + totalFile + "处"; } sw.Close(); MessageBox.Show("操作成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } } catch (Exception ee) { MessageBox.Show("操作失败:" + ee.Message); }
------解决方案--------------------
把word文档当二进制流存进数据库,绝对原汁原味