代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
CSharp
】
C#读取HTML文件内容写入记事本
作者:
Dezai.CN
/ 发布于
2011/11/29
/
544
<div><span style="color: rgb(0,0,255)">try</span> { <span style="color: rgb(0,0,255)">int</span> totalFile = <span style="color: rgb(128,0,128)">0</span>; <span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">string dirPath = @"E:\chfuMetarnet\BSC6810 alarm\";</span><span style="color: rgb(0,128,0)"> </span> <span style="color: rgb(0,0,255)">if</span> (<span style="color: rgb(0,0,255)">this</span>.textBox1.Text.Trim() == <span style="color: rgb(128,0,0)">""</span>) { MessageBox.Show(<span style="color: rgb(128,0,0)">"</span><span style="color: rgb(128,0,0)">请输入HTML文件路径!</span><span style="color: rgb(128,0,0)">"</span>); } <span style="color: rgb(0,0,255)">else</span> { <span style="color: rgb(0,0,255)">string</span> dirPath = <span style="color: rgb(0,0,255)">this</span>.textBox1.Text.Trim(); <span style="color: rgb(0,0,255)">if</span> (!dirPath.Substring(dirPath.Length - <span style="color: rgb(128,0,128)">1</span>).Contains(<span style="color: rgb(128,0,0)">"</span><span style="color: rgb(128,0,0)">\\</span><span style="color: rgb(128,0,0)">"</span>)) { dirPath = dirPath+<span style="color: rgb(128,0,0)">"</span><span style="color: rgb(128,0,0)">\\</span><span style="color: rgb(128,0,0)">"</span>; } StreamWriter sw; DirectoryInfo dirInfo = <span style="color: rgb(0,0,255)">new</span> DirectoryInfo(dirPath); FileInfo[] files = dirInfo.GetFiles(); <span style="color: rgb(0,0,255)">string</span> filename = dirPath + <span style="color: rgb(128,0,0)">"</span><span style="color: rgb(128,0,0)">告警经验库信息.txt</span><span style="color: rgb(128,0,0)">"</span>; <span style="color: rgb(0,0,255)">if</span> (File.Exists(filename)) { sw = File.AppendText(filename); } <span style="color: rgb(0,0,255)">else</span> { sw = File.CreateText(filename); } <span style="color: rgb(0,0,255)">foreach</span> (FileInfo fileinfo <span style="color: rgb(0,0,255)">in</span> files) { <span style="color: rgb(0,0,255)">if</span> (fileinfo.Extension.Equals(<span style="color: rgb(128,0,0)">"</span><span style="color: rgb(128,0,0)">.htm</span><span style="color: rgb(128,0,0)">"</span>))<span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">遍历所有htm文件</span><span style="color: rgb(0,128,0)"> </span> { totalFile = totalFile + <span style="color: rgb(128,0,128)">1</span>; WebRequest myWebRequest = WebRequest.Create(dirPath + fileinfo.Name); WebResponse myWebResponse = myWebRequest.GetResponse(); Stream myStream = myWebResponse.GetResponseStream(); Encoding encode = System.Text.Encoding.GetEncoding(<span style="color: rgb(128,0,0)">"</span><span style="color: rgb(128,0,0)">gb2312</span><span style="color: rgb(128,0,0)">"</span>); StreamReader myStreamReader = <span style="color: rgb(0,0,255)">new</span> StreamReader(myStream, encode); <span style="color: rgb(0,0,255)">string</span> strhtml = myStreamReader.ReadToEnd(); myWebResponse.Close(); <span style="color: rgb(0,0,255)">string</span> stroutput = strhtml; Regex regex = <span style="color: rgb(0,0,255)">new</span> Regex(<span style="color: rgb(128,0,0)">@"</span><span style="color: rgb(128,0,0)"><[^>]+>|</[^>]+></span><span style="color: rgb(128,0,0)">"</span>);<span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">去掉HTML标记的正则表达式</span><span style="color: rgb(0,128,0)"> </span> <span style="color: rgb(0,0,255)">string</span> tmpStr = <span style="color: rgb(128,0,0)">"</span><span style="color: rgb(128,0,0)"><h4>([^<]*)</h4></span><span style="color: rgb(128,0,0)">"</span>; <span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">获取<h4>之间内容的表达式</span><span style="color: rgb(0,128,0)"> </span> Match TitleMatch = Regex.Match(strhtml, tmpStr, RegexOptions.IgnoreCase | RegexOptions.Multiline); <span style="color: rgb(0,0,255)">string</span> causename = TitleMatch.Value.ToString();<span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">包含<h4>和</h4>标记</span><span style="color: rgb(0,128,0)"> </span> causename = Regex.Replace(causename, <span style="color: rgb(128,0,0)">"</span><span style="color: rgb(128,0,0)">[\n|\r|\t]</span><span style="color: rgb(128,0,0)">"</span>, <span style="color: rgb(128,0,0)">"</span><span style="color: rgb(128,0,0)"> </span><span style="color: rgb(128,0,0)">"</span>);<span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">去掉换行和TAB键符号</span><span style="color: rgb(0,128,0)"> </span> causename = causename.Trim(); <span style="color: rgb(0,0,255)">string</span> cause = causename.Substring(<span style="color: rgb(128,0,128)">4</span>, causename.Length - <span style="color: rgb(128,0,128)">9</span>);<span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">得到告警原因</span><span style="color: rgb(0,128,0)"> </span> <span style="color: rgb(0,0,255)">string</span> titleStr = <span style="color: rgb(128,0,0)">"</span><span style="color: rgb(128,0,0)"><title>([^<]*)</title></span><span style="color: rgb(128,0,0)">"</span>; TitleMatch = Regex.Match(strhtml, titleStr, RegexOptions.IgnoreCase | RegexOptions.Multiline); <span style="color: rgb(0,0,255)">string</span> titlename = TitleMatch.Value.ToString(); titlename = Regex.Replace(titlename, <span style="color: rgb(128,0,0)">"</span><span style="color: rgb(128,0,0)">[\n|\r|\t]</span><span style="color: rgb(128,0,0)">"</span>, <span style="color: rgb(128,0,0)">""</span>);<span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">去掉换行和TAB键符号</span><span style="color: rgb(0,128,0)"> </span> titlename = titlename.Trim(); <span style="color: rgb(0,0,255)">string</span> regexStr = <span style="color: rgb(128,0,0)">"</span><span style="color: rgb(128,0,0)"><ul><li>(?<key>.*?)</ul></span><span style="color: rgb(128,0,0)">"</span>;<span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">获取<ul><li>后边的内容,直到</ul>结尾</span><span style="color: rgb(0,128,0)"> </span> Regex r = <span style="color: rgb(0,0,255)">new</span> Regex(regexStr, RegexOptions.None); strhtml = Regex.Replace(strhtml, <span style="color: rgb(128,0,0)">"</span><span style="color: rgb(128,0,0)">[\n|\r|\t]</span><span style="color: rgb(128,0,0)">"</span>, <span style="color: rgb(128,0,0)">""</span>);<span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">去掉换行和TAB键符号</span><span style="color: rgb(0,128,0)"> </span> Match mc = r.Match(strhtml); <span style="color: rgb(0,0,255)">string</span> dataStr = mc.Groups[<span style="color: rgb(128,0,0)">"</span><span style="color: rgb(128,0,0)">key</span><span style="color: rgb(128,0,0)">"</span>].Value; dataStr = <span style="color: rgb(128,0,0)">"</span><span style="color: rgb(128,0,0)"><ul><li></span><span style="color: rgb(128,0,0)">"</span> + dataStr + <span style="color: rgb(128,0,0)">"</span><span style="color: rgb(128,0,0)"></ul></span><span style="color: rgb(128,0,0)">"</span>;<span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">得到完整的<ul></ul>之间的源码</span><span style="color: rgb(0,128,0)"> </span> strhtml = strhtml.Replace(dataStr, <span style="color: rgb(128,0,0)">""</span>);<span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">将去掉换行符和tab键的源码中去除<ul></ul>部分源码</span><span style="color: rgb(0,128,0)"> </span> strhtml = strhtml.Replace(titlename, <span style="color: rgb(128,0,0)">""</span>);<span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">去掉<title></title></span><span style="color: rgb(0,128,0)"> </span> strhtml = regex.Replace(strhtml, <span style="color: rgb(128,0,0)">"</span><span style="color: rgb(128,0,0)"> </span><span style="color: rgb(128,0,0)">"</span>);<span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">过滤掉HTML标记</span><span style="color: rgb(0,128,0)"> </span> strhtml = strhtml.Replace(<span style="color: rgb(128,0,0)">"</span><span style="color: rgb(128,0,0)"> </span><span style="color: rgb(128,0,0)">"</span>, <span style="color: rgb(128,0,0)">""</span>);<span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">去掉空格字符</span><span style="color: rgb(0,128,0)"> </span> <span style="color: rgb(0,0,255)">string</span>[] arr = cause.Split(<span style="color: rgb(128,0,0)">'</span><span style="color: rgb(128,0,0)"> </span><span style="color: rgb(128,0,0)">'</span>); <span style="color: rgb(0,0,255)">string</span> zhCause = arr[arr.Length - <span style="color: rgb(128,0,128)">1</span>];<span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">获取数组最后一个元素:告警原因</span><span style="color: rgb(0,128,0)"> </span> sw.WriteLine(<span style="color: rgb(128,0,0)">"</span><span style="color: rgb(128,0,0)">第</span><span style="color: rgb(128,0,0)">"</span> + totalFile + <span style="color: rgb(128,0,0)">"</span><span style="color: rgb(128,0,0)">个文件:</span><span style="color: rgb(128,0,0)">"</span> + fileinfo.Name); sw.WriteLine(<span style="color: rgb(128,0,0)">"</span><span style="color: rgb(128,0,0)">-----告警原因------:</span><span style="color: rgb(128,0,0)">"</span>); <span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">sw.WriteLine(cause);</span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)"> ALM-1 网元启动</span><span style="color: rgb(0,128,0)"> </span> zhCause=<span style="color: rgb(0,0,255)">this</span>.chinaString(zhCause); sw.WriteLine(zhCause);<span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">网元启动</span><span style="color: rgb(0,128,0)"> </span> sw.WriteLine(<span style="color: rgb(128,0,0)">"</span><span style="color: rgb(128,0,0)">-----处理经验------:</span><span style="color: rgb(128,0,0)">"</span>); sw.WriteLine(strhtml); sw.WriteLine(); sw.Flush(); } } sw.Close(); MessageBox.Show(<span style="color: rgb(128,0,0)">"</span><span style="color: rgb(128,0,0)">操作成功!</span><span style="color: rgb(128,0,0)">"</span>, <span style="color: rgb(128,0,0)">"</span><span style="color: rgb(128,0,0)">提示</span><span style="color: rgb(128,0,0)">"</span>, MessageBoxButtons.OK, MessageBoxIcon.Information); } } <span style="color: rgb(0,0,255)">catch</span> (Exception ee) { MessageBox.Show(<span style="color: rgb(128,0,0)">"</span><span style="color: rgb(128,0,0)">操作失败:</span><span style="color: rgb(128,0,0)">"</span> + ee.Message); }
试试其它关键字
读取HTML文件内容
同语言下
.
文件IO 操作类库
.
Check图片类型[JPEG(.jpg 、.jpeg),TIF,GIF,BMP,PNG,P
.
机器名和IP取得(IPV4 IPV6)
.
Tiff转换Bitmap
.
linqHelper
.
MadieHelper.cs
.
RegHelper.cs
.
如果关闭一个窗体后激活另一个窗体的事件或方法
.
创建日志通用类
.
串口辅助开发类
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
Dezai.CN
贡献的其它代码
(
4037
)
.
多线程Socket服务器模块
.
生成随机密码
.
清除浮动样式
.
弹出窗口居中
.
抓取url的函数
.
使用base HTTP验证
.
div模拟iframe嵌入效果
.
通过header转向的方法
.
Session操作类
.
执行sqlite输入插入操作后获得自动编号的ID
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3