当前位置:   article > 正文

c# 常用类库_c# \u001b

c# \u001b
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.IO;
  6. using System.Net;
  7. using System.Text;
  8. using System.Runtime.Serialization;
  9. using System.Runtime.Serialization.Json;
  10. using System.Text.RegularExpressions;
  11. using HtmlAgilityPack;
  12. using System.Collections;
  13. using System.Web.UI;
  14. using System.Globalization;
  15. namespace Common
  16. {
  17. /// <summary>
  18. /// 常用工具类
  19. /// </summary>
  20. public class Utils
  21. {
  22. /// <summary>
  23. /// 读取本地磁盘文件内容
  24. /// </summary>
  25. /// <param name="url">文件路径</param>
  26. /// <returns></returns>
  27. public static string GetFileSource(string path)
  28. {
  29. try
  30. {
  31. using (StreamReader myFile = new StreamReader(System.Web.HttpContext.Current.Server.MapPath(path), Encoding.Default))
  32. { return myFile.ReadToEnd(); }
  33. }
  34. catch { return ""; }
  35. }
  36. /// <summary>
  37. /// 生成Json格式
  38. /// 首先应添加对System.ServiceModel.Web,System.Runtime.Serialization的引用,
  39. /// 然后添加System.Runtime.Serialization.Json命名空间
  40. /// </summary>
  41. /// <typeparam name="T"></typeparam>
  42. /// <param name="obj"></param>
  43. /// <returns></returns>
  44. public static string GetJson<T>(T obj)
  45. {
  46. DataContractJsonSerializer json = new DataContractJsonSerializer(obj.GetType());
  47. using (MemoryStream stream = new MemoryStream())
  48. {
  49. json.WriteObject(stream, obj);
  50. string szJson = Encoding.UTF8.GetString(stream.ToArray()); return szJson;
  51. }
  52. }
  53. /// <summary>
  54. /// 获取Json的Model
  55. /// </summary>
  56. /// <typeparam name="T"></typeparam>
  57. /// <param name="szJson"></param>
  58. /// <returns></returns>
  59. public static T ParseFromJson<T>(string szJson)
  60. {
  61. T obj = Activator.CreateInstance<T>();
  62. using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(szJson)))
  63. {
  64. DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType());
  65. return (T)serializer.ReadObject(ms);
  66. }
  67. }
  68. /// <summary>
  69. /// 返回文件是否存在
  70. /// </summary>
  71. /// <param name="filename">文件名</param>
  72. /// <returns>是否存在</returns>
  73. public static bool FileExists(string filename)
  74. {
  75. return File.Exists(filename);
  76. }
  77. /// <summary>
  78. /// 过滤字符串
  79. /// </summary>
  80. /// <param name="str"></param>
  81. /// <returns></returns>
  82. public static string FileterStr(string str)
  83. {
  84. if (!string.IsNullOrWhiteSpace(str))
  85. {
  86. str = Regex.Replace(str, @"(<script>)", "<script>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
  87. str = Regex.Replace(str, @"(</script>)", "</script>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
  88. str = Regex.Replace(str, @"( ){4,}", "    ", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
  89. str = Regex.Replace(str, @"(<br>){1,}|(<br/>){1,}", "<br>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
  90. str = Regex.Replace(str, @"(<br>){1,}", "<br>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
  91. }
  92. else
  93. { str = ""; }
  94. return str;
  95. }
  96. /// <summary>
  97. /// 过滤Script字符串
  98. /// </summary>
  99. /// <param name="str"></param>
  100. /// <returns></returns>
  101. public static string FileterScript(string str)
  102. {
  103. if (!string.IsNullOrWhiteSpace(str))
  104. {
  105. str = Regex.Replace(str, @"(<script>)", "<script>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
  106. str = Regex.Replace(str, @"(</script>)", "</script>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
  107. }
  108. else
  109. { str = ""; }
  110. return str;
  111. }
  112. /// <summary>
  113. /// 获得当前绝对路径
  114. /// </summary>
  115. /// <param name="strPath">指定的路径</param>
  116. /// <returns>绝对路径</returns>
  117. public static string GetMapPath(string strPath)
  118. {
  119. if (HttpContext.Current != null)
  120. {
  121. return HttpContext.Current.Server.MapPath(strPath);
  122. }
  123. else //非web程序引用
  124. {
  125. strPath = strPath.Replace("/", "\\");
  126. if (strPath.StartsWith("\\"))
  127. {
  128. strPath = strPath.Substring(strPath.IndexOf('\\', 1)).TrimStart('\\');
  129. }
  130. return System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, strPath);
  131. }
  132. }
  133. /// <summary>
  134. /// 得到网站的真实路径
  135. /// </summary>
  136. /// <returns></returns>
  137. public static string GetTrueWebPath()
  138. {
  139. string webPath = HttpContext.Current.Request.Path;
  140. if (webPath.LastIndexOf("/") != webPath.IndexOf("/"))
  141. webPath = webPath.Substring(webPath.IndexOf("/"), webPath.LastIndexOf("/") + 1);
  142. else
  143. webPath = "/";
  144. return webPath;
  145. }
  146. /// <summary>
  147. /// 获取站点根目录URL
  148. /// </summary>
  149. /// <returns></returns>
  150. public static string GetRootUrl(string forumPath)
  151. {
  152. int port = HttpContext.Current.Request.Url.Port;
  153. return string.Format("{0}://{1}{2}{3}",
  154. HttpContext.Current.Request.Url.Scheme,
  155. HttpContext.Current.Request.Url.Host.ToString(),
  156. (port == 80 || port == 0) ? "" : ":" + port,
  157. forumPath);
  158. }
  159. /// <summary>
  160. /// 验证文件名
  161. /// </summary>
  162. private static readonly char[] InvalidFileNameChars = new[]
  163. {
  164. '"',
  165. '<',
  166. '>',
  167. '|',
  168. '\0',
  169. '\u0001',
  170. '\u0002',
  171. '\u0003',
  172. '\u0004',
  173. '\u0005',
  174. '\u0006',
  175. '\a',
  176. '\b',
  177. '\t',
  178. '\n',
  179. '\v',
  180. '\f',
  181. '\r',
  182. '\u000e',
  183. '\u000f',
  184. '\u0010',
  185. '\u0011',
  186. '\u0012',
  187. '\u0013',
  188. '\u0014',
  189. '\u0015',
  190. '\u0016',
  191. '\u0017',
  192. '\u0018',
  193. '\u0019',
  194. '\u001a',
  195. '\u001b',
  196. '\u001c',
  197. '\u001d',
  198. '\u001e',
  199. '\u001f',
  200. ':',
  201. '*',
  202. '?',
  203. '\\',
  204. '/'
  205. };
  206. public static string CleanInvalidFileName(string fileName)
  207. {
  208. fileName = fileName + "";
  209. fileName = InvalidFileNameChars.Aggregate(fileName, (current, c) => current.Replace(c + "", ""));
  210. if (fileName.Length > 1)
  211. if (fileName[0] == '.')
  212. fileName = "dot" + fileName.TrimStart('.');
  213. return fileName;
  214. }
  215. /// <summary>
  216. /// 将远程图片保存到服务器
  217. /// </summary>
  218. /// <param name="remotePath"></param>
  219. /// <param name="filePath"></param>
  220. public static string DownLoadImg(string url, string folderPath, bool isoldname = false)
  221. {
  222. try
  223. {
  224. string fileName = "";
  225. fileName = url.Substring(url.LastIndexOf("/") + 1);
  226. if (fileName.IndexOf("?") > -1)
  227. {
  228. fileName = fileName.Substring(0, fileName.IndexOf("?"));
  229. }
  230. fileName = CleanInvalidFileName(fileName);
  231. if (!isoldname)
  232. {
  233. string extension = fileName.Split('.')[1];
  234. // 生成随机文件名
  235. Random random = new Random(DateTime.Now.Millisecond);
  236. fileName = DateTime.Now.ToString("yyyyMMddHHmmss_ffff", DateTimeFormatInfo.InvariantInfo) + "." + extension;
  237. }
  238. var fileFolder = HttpContext.Current.Server.MapPath(folderPath);
  239. DownloadImage(url, fileFolder + fileName);
  240. return folderPath + fileName;
  241. }
  242. catch
  243. {
  244. return "";
  245. }
  246. }
  247. /// <summary>
  248. /// 将远程图片保存到服务器
  249. /// </summary>
  250. /// <param name="remotePath"></param>
  251. /// <param name="filePath"></param>
  252. public static void DownloadImage(string remotePath, string filePath)
  253. {
  254. WebClient w = new WebClient();
  255. try
  256. {
  257. w.DownloadFile(remotePath, filePath);
  258. }
  259. finally
  260. {
  261. w.Dispose();
  262. }
  263. }
  264. /// <summary>
  265. /// 把内容中的图片下载到本地
  266. /// </summary>
  267. /// <param name="htmlContent"></param>
  268. /// <returns></returns>
  269. public static string DownloadImages(string htmlContent, string folderPath,string webUrl)
  270. {
  271. string newContent = htmlContent;
  272. //实例化HtmlAgilityPack.HtmlDocument对象
  273. HtmlDocument doc = new HtmlDocument();
  274. //载入HTML
  275. doc.LoadHtml(htmlContent);
  276. var imgs = doc.DocumentNode.SelectNodes("//img");
  277. if (imgs != null && imgs.Count > 0)
  278. {
  279. foreach (HtmlNode child in imgs)
  280. {
  281. if (child.Attributes["src"] == null)
  282. continue;
  283. string imgurl = child.Attributes["src"].Value;
  284. if (imgurl.IndexOf(webUrl) > -1 || imgurl.IndexOf("http://") == -1)
  285. continue;
  286. string newimgurl = DownLoadImg(imgurl, folderPath);
  287. if (newimgurl != "")
  288. {
  289. newContent=newContent.Replace(imgurl, webUrl + newimgurl);
  290. }
  291. }
  292. }
  293. return newContent;
  294. }
  295. /// <summary>
  296. /// 刷新图片地址,加上指定url前缀
  297. /// </summary>
  298. /// <param name="htmlContent"></param>
  299. /// <returns></returns>
  300. public static string RefreshImageUrl(string htmlContent, string webUrl)
  301. {
  302. string newContent = htmlContent;
  303. HtmlDocument doc = new HtmlDocument();
  304. doc.LoadHtml(htmlContent);
  305. var imgs = doc.DocumentNode.SelectNodes("//img");
  306. if (imgs != null && imgs.Count > 0)
  307. {
  308. foreach (HtmlNode child in imgs)
  309. {
  310. if (child.Attributes["src"] == null)
  311. continue;
  312. string imgurl = child.Attributes["src"].Value;
  313. if (imgurl.IndexOf(webUrl) > -1 || imgurl.IndexOf("http://") > -1)
  314. continue;
  315. string newimgurl = webUrl + imgurl;
  316. newContent = newContent.Replace(imgurl, newimgurl);
  317. }
  318. }
  319. return newContent;
  320. }
  321. /// <summary>
  322. /// 获得当前页面客户端的IP
  323. /// </summary>
  324. /// <returns>当前页面客户端的IP</returns>
  325. public static string GetIP()
  326. {
  327. string result = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
  328. if (string.IsNullOrEmpty(result))
  329. result = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
  330. if (string.IsNullOrEmpty(result))
  331. result = HttpContext.Current.Request.UserHostAddress;
  332. if (string.IsNullOrEmpty(result) || !IsIP(result))
  333. return "127.0.0.1";
  334. return result;
  335. }
  336. /// <summary>
  337. /// 是否为ip
  338. /// </summary>
  339. /// <param name="ip"></param>
  340. /// <returns></returns>
  341. public static bool IsIP(string ip)
  342. {
  343. return Regex.IsMatch(ip, @"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$");
  344. }
  345. private static Regex RegexBr = new Regex(@"(\r\n)", RegexOptions.IgnoreCase);
  346. /// <summary>
  347. /// 清除给定字符串中的回车及换行符
  348. /// </summary>
  349. /// <param name="str">要清除的字符串</param>
  350. /// <returns>清除后返回的字符串</returns>
  351. public static string ClearBR(string str)
  352. {
  353. Match m = null;
  354. for (m = RegexBr.Match(str); m.Success; m = m.NextMatch())
  355. {
  356. str = str.Replace(m.Groups[0].ToString(), "");
  357. }
  358. return str;
  359. }
  360. public static Regex regImg = new Regex(@"<img\b[^<>]*?\bsrc[\s\t\r\n]*=[\s\t\r\n]*[""']?[\s\t\r\n]*(?<imgUrl>[^\s\t\r\n""'<>]*)[^<>]*?/?[\s\t\r\n]*>", RegexOptions.IgnoreCase);
  361. /// <summary>
  362. /// 获得指定内容中的第一张图片
  363. /// </summary>
  364. /// <returns></returns>
  365. public static string GetImg(string str)
  366. {
  367. Match m = regImg.Match(str);
  368. if (m.Success)
  369. return m.Groups["imgUrl"].ToString();
  370. return "";
  371. }
  372. /// <summary>
  373. /// 获得指定内容中的所有图片
  374. /// </summary>
  375. /// <returns></returns>
  376. public static string GetImgs(string str)
  377. {
  378. string strTemp = "";
  379. MatchCollection matches = regImg.Matches(str);
  380. foreach (Match match in matches)
  381. {
  382. strTemp += "'" + match.Groups["imgUrl"].Value + "',";
  383. }
  384. return strTemp.Trim(',');
  385. }
  386. /// <summary>
  387. /// 删除指定内容中的所有图片
  388. /// </summary>
  389. /// <returns></returns>
  390. public static string ClearImgs(string str)
  391. {
  392. Match m = null;
  393. for (m = regImg.Match(str); m.Success; m = m.NextMatch())
  394. {
  395. str = str.Replace(m.Groups[0].ToString(), "");
  396. }
  397. return str;
  398. }
  399. /// <summary>
  400. /// 根据Url获得源文件内容
  401. /// </summary>
  402. /// <param name="url">合法的Url地址</param>
  403. /// <returns></returns>
  404. public static string GetSourceTextByUrl(string url)
  405. {
  406. try
  407. {
  408. WebRequest request = WebRequest.Create(url);
  409. request.Timeout = 20000;//20秒超时
  410. WebResponse response = request.GetResponse();
  411. Stream resStream = response.GetResponseStream();
  412. StreamReader sr = new StreamReader(resStream);
  413. return sr.ReadToEnd();
  414. }
  415. catch { return ""; }
  416. }
  417. /// <summary>
  418. /// 访问指定url
  419. /// </summary>
  420. /// <param name="url">合法的Url地址</param>
  421. /// <returns></returns>
  422. public static void AccessUrl(string url)
  423. {
  424. try
  425. {
  426. WebRequest request = WebRequest.Create(url);
  427. request.Timeout = 20000;//20秒超时
  428. WebResponse response = request.GetResponse();
  429. }
  430. catch { }
  431. }
  432. /// <summary>
  433. /// 移除Html标记
  434. /// </summary>
  435. /// <param name="content"></param>
  436. /// <returns></returns>
  437. public static string RemoveHtml(string content)
  438. {
  439. return Regex.Replace(content, @"<[^>]*>", string.Empty, RegexOptions.IgnoreCase);
  440. }
  441. /// <summary>
  442. /// 过滤HTML中的不安全标签
  443. /// </summary>
  444. /// <param name="content"></param>
  445. /// <returns></returns>
  446. public static string RemoveUnsafeHtml(string content)
  447. {
  448. content = Regex.Replace(content, @"(\<|\s+)o([a-z]+\s?=)", "$1$2", RegexOptions.IgnoreCase);
  449. content = Regex.Replace(content, @"(script|frame|form|meta|behavior|style)([\s|:|>])+", "$1.$2", RegexOptions.IgnoreCase);
  450. return content;
  451. }
  452. /// <summary>
  453. /// 从HTML中获取文本,保留br,p,img
  454. /// </summary>
  455. /// <param name="HTML"></param>
  456. /// <returns></returns>
  457. public static string GetTextFromHTML(string HTML)
  458. {
  459. System.Text.RegularExpressions.Regex regEx = new System.Text.RegularExpressions.Regex(@"</?(?!br|/?p|img)[^>]*>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
  460. return regEx.Replace(HTML, "");
  461. }
  462. /// <summary>
  463. /// 根据字符串获取枚举值
  464. /// </summary>
  465. /// <typeparam name="T">枚举类型</typeparam>
  466. /// <param name="value">字符串枚举值</param>
  467. /// <param name="defValue">缺省值</param>
  468. /// <returns></returns>
  469. public static T GetEnum<T>(string value, T defValue)
  470. {
  471. try
  472. {
  473. return (T)Enum.Parse(typeof(T), value, true);
  474. }
  475. catch (ArgumentException)
  476. {
  477. return defValue;
  478. }
  479. }
  480. /// <summary>
  481. /// 删除字符串尾部的回车/换行/空格
  482. /// </summary>
  483. /// <param name="str"></param>
  484. /// <returns></returns>
  485. public static string RTrim(string str)
  486. {
  487. for (int i = str.Length; i >= 0; i--)
  488. {
  489. if (str[i].Equals(" ") || str[i].Equals("\r") || str[i].Equals("\n"))
  490. {
  491. str.Remove(i, 1);
  492. }
  493. }
  494. return str;
  495. }
  496. /// <summary>
  497. /// 从字符串的指定位置截取指定长度的子字符串
  498. /// </summary>
  499. /// <param name="str">原字符串</param>
  500. /// <param name="startIndex">子字符串的起始位置</param>
  501. /// <param name="length">子字符串的长度</param>
  502. /// <returns>子字符串</returns>
  503. public static string CutString(string str, int startIndex, int length)
  504. {
  505. if (startIndex >= 0)
  506. {
  507. if (length < 0)
  508. {
  509. length = length * -1;
  510. if (startIndex - length < 0)
  511. {
  512. length = startIndex;
  513. startIndex = 0;
  514. }
  515. else
  516. startIndex = startIndex - length;
  517. }
  518. if (startIndex > str.Length)
  519. return "";
  520. }
  521. else
  522. {
  523. if (length < 0)
  524. return "";
  525. else
  526. {
  527. if (length + startIndex > 0)
  528. {
  529. length = length + startIndex;
  530. startIndex = 0;
  531. }
  532. else
  533. return "";
  534. }
  535. }
  536. if (str.Length - startIndex < length)
  537. length = str.Length - startIndex;
  538. return str.Substring(startIndex, length);
  539. }
  540. /// <summary>
  541. /// 从字符串的指定位置开始截取到字符串结尾的了符串
  542. /// </summary>
  543. /// <param name="str">原字符串</param>
  544. /// <param name="startIndex">子字符串的起始位置</param>
  545. /// <returns>子字符串</returns>
  546. public static string CutString(string str, int startIndex)
  547. {
  548. return CutString(str, startIndex, str.Length);
  549. }
  550. /// <summary>
  551. /// 将对象转换为Int32类型
  552. /// </summary>
  553. /// <param name="strValue">要转换的字符串</param>
  554. /// <param name="defValue">缺省值</param>
  555. /// <returns>转换后的int类型结果</returns>
  556. public static int ObjectToInt(object expression)
  557. {
  558. return ObjectToInt(expression, 0);
  559. }
  560. /// <summary>
  561. /// 将对象转换为Int32类型
  562. /// </summary>
  563. /// <param name="strValue">要转换的字符串</param>
  564. /// <param name="defValue">缺省值</param>
  565. /// <returns>转换后的int类型结果</returns>
  566. public static int ObjectToInt(object expression, int defValue)
  567. {
  568. if (expression != null)
  569. return StrToInt(expression.ToString(), defValue);
  570. return defValue;
  571. }
  572. /// <summary>
  573. /// 将对象转换为Int32类型,转换失败返回0
  574. /// </summary>
  575. /// <param name="str">要转换的字符串</param>
  576. /// <returns>转换后的int类型结果</returns>
  577. public static int StrToInt(string str)
  578. {
  579. return StrToInt(str, 0);
  580. }
  581. /// <summary>
  582. /// 将对象转换为Int32类型
  583. /// </summary>
  584. /// <param name="str">要转换的字符串</param>
  585. /// <param name="defValue">缺省值</param>
  586. /// <returns>转换后的int类型结果</returns>
  587. public static int StrToInt(string str, int defValue)
  588. {
  589. if (string.IsNullOrEmpty(str) || str.Trim().Length >= 11 || !Regex.IsMatch(str.Trim(), @"^([-]|[0-9])[0-9]*(\.\w*)?$"))
  590. return defValue;
  591. int rv;
  592. if (Int32.TryParse(str, out rv))
  593. return rv;
  594. return Convert.ToInt32(StrToFloat(str, defValue));
  595. }
  596. /// <summary>
  597. /// string型转换为float型
  598. /// </summary>
  599. /// <param name="strValue">要转换的字符串</param>
  600. /// <param name="defValue">缺省值</param>
  601. /// <returns>转换后的int类型结果</returns>
  602. public static float StrToFloat(string strValue, float defValue)
  603. {
  604. if ((strValue == null) || (strValue.Length > 10))
  605. return defValue;
  606. float intValue = defValue;
  607. if (strValue != null)
  608. {
  609. bool IsFloat = Regex.IsMatch(strValue, @"^([-]|[0-9])[0-9]*(\.\w*)?$");
  610. if (IsFloat)
  611. float.TryParse(strValue, out intValue);
  612. }
  613. return intValue;
  614. }
  615. /// <summary>
  616. /// 获得指定数量的符号字符串
  617. /// </summary>
  618. /// <param name="count"></param>
  619. /// <param name="str"></param>
  620. /// <returns></returns>
  621. public static string GetSpace(int count, string str)
  622. {
  623. string tmp = "";
  624. for (int i = 1; i < count; i++)
  625. {
  626. tmp += str;
  627. }
  628. return tmp;
  629. }
  630. /// <summary>
  631. /// 清除字符串数组中的重复项
  632. /// </summary>
  633. /// <param name="strArray">字符串数组</param>
  634. /// <param name="maxElementLength">字符串数组中单个元素的最大长度</param>
  635. /// <returns></returns>
  636. public static string[] DistinctStringArray(string[] strArray, int maxElementLength)
  637. {
  638. Hashtable h = new Hashtable();
  639. foreach (string s in strArray)
  640. {
  641. string k = s;
  642. if (maxElementLength > 0 && k.Length > maxElementLength)
  643. {
  644. k = k.Substring(0, maxElementLength);
  645. }
  646. h[k.Trim()] = s;
  647. }
  648. string[] result = new string[h.Count];
  649. h.Keys.CopyTo(result, 0);
  650. return result;
  651. }
  652. /// <summary>
  653. /// 清除字符串数组中的重复项
  654. /// </summary>
  655. /// <param name="strArray">字符串数组</param>
  656. /// <returns></returns>
  657. public static string[] DistinctStringArray(string[] strArray)
  658. {
  659. return DistinctStringArray(strArray, 0);
  660. }
  661. /// <summary>
  662. /// 获取字符串a在b中出现的次数
  663. /// </summary>
  664. public static int RegexCount(string regstr, string str)
  665. {
  666. Regex r = new Regex(regstr, RegexOptions.Singleline | RegexOptions.IgnoreCase);
  667. MatchCollection m = r.Matches(str);
  668. return m.Count;
  669. }
  670. /// <summary>
  671. /// 消除无效的XML编码字符
  672. /// </summary>
  673. public static string CleanInvalidXmlChars(string text)
  674. {
  675. // From xml spec valid chars:
  676. // #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] | [#x10000-#x10FFFF]
  677. // any Unicode character, excluding the surrogate blocks, FFFE, and FFFF.
  678. string re = @"[^\x09\x0A\x0D\x20-\xD7FF\xE000-\xFFFD\x10000-x10FFFF]";
  679. return Regex.Replace(text, re, "");
  680. }
  681. /// <summary>
  682. /// web页转换为静态html
  683. /// </summary>
  684. public static void CreateHtml(string url, string outpath)
  685. {
  686. FileStream fs;
  687. if (File.Exists(outpath))
  688. {
  689. File.Delete(outpath);
  690. fs = File.Create(outpath);
  691. }
  692. else
  693. {
  694. fs = File.Create(outpath);
  695. }
  696. byte[] bt = Encoding.UTF8.GetBytes(GetSourceTextByUrl(url));
  697. fs.Write(bt, 0, bt.Length);
  698. fs.Close();
  699. }
  700. /// <summary>
  701. /// 删除html
  702. /// </summary>
  703. public static void DeleteHtml(string path)
  704. {
  705. if (File.Exists(path))
  706. {
  707. File.Delete(path);
  708. }
  709. }
  710. //创建文件夹
  711. public static void CreateFolder(string FolderPath)
  712. {
  713. if (!System.IO.Directory.Exists(FolderPath)) System.IO.Directory.CreateDirectory(FolderPath);
  714. }
  715. }
  716. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/228633
推荐阅读
相关标签
  

闽ICP备14008679号