C# 批量更换html里面的http链接

打印 上一主题 下一主题

主题 1053|帖子 1053|积分 3159

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
C# 批量更换html里面的http链接

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Text.RegularExpressions;
  10. using System.Threading.Tasks;
  11. using System.Windows.Forms;
  12. using static System.Windows.Forms.VisualStyles.VisualStyleElement.ProgressBar;
  13. namespace 批量替换文件html
  14. {
  15.     public partial class Form1 : Form
  16.     {
  17.         public Form1()
  18.         {
  19.             InitializeComponent();
  20.         }
  21.         private void btn_chose_Click(object sender, EventArgs e)
  22.         {
  23.             string selectedFolderPath = SelectFolder();
  24.             if (!string.IsNullOrEmpty(selectedFolderPath))
  25.             {
  26.                 // 用户选择了文件夹,并且可以使用selectedFolderPath
  27.                 //MessageBox.Show("选择的文件夹路径: " + selectedFolderPath);
  28.                 tbx_LJ.Text = selectedFolderPath;
  29.             }
  30.             else
  31.             {
  32.                 MessageBox.Show("没有选择文件夹");
  33.             }
  34.         }
  35.         public static string SelectFolder()
  36.         {
  37.             using (FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog())
  38.             {
  39.                 folderBrowserDialog.Description = "请选择一个文件夹";
  40.                 DialogResult dialogResult = folderBrowserDialog.ShowDialog();
  41.                 if (dialogResult == DialogResult.OK)
  42.                 {
  43.                     return folderBrowserDialog.SelectedPath;
  44.                 }
  45.                 else
  46.                 {
  47.                     return string.Empty;
  48.                 }
  49.             }
  50.         }
  51.         public int count = 0;
  52.         private void button1_Click(object sender, EventArgs e)
  53.         {
  54.             string folderPath = @tbx_LJ.Text;
  55.             string[] htmlFiles = Directory.GetFiles(folderPath, "*.html", SearchOption.AllDirectories);
  56.             foreach (string file in htmlFiles)
  57.             {
  58.                 textBox1.AppendText(file+"\r\n");
  59.                 count++;
  60.                 lbl_count.Text = count.ToString() + "个";
  61.                 Application.DoEvents();
  62.                 //timer1.Enabled = true;
  63.                 string content = File.ReadAllText(file);
  64.                 content = Regex.Replace(content, @"href='https:\/\/[^""]+", "href="", RegexOptions.IgnoreCase);
  65.                 // // 删除href属性中的http链接
  66.                 content = Regex.Replace(content, @"href=""http:\/\/[^""]+", "href="", RegexOptions.IgnoreCase);
  67.                 // //删除style属性中的url里的http链接
  68.                 content = Regex.Replace(content, @"url\(http:\/\/[^)]+\)", "url(#)", RegexOptions.IgnoreCase);
  69.                 // //删除style属性中的url里的http链接
  70.                 content = Regex.Replace(content, @"src=""http:\/\/[^""]+", "src="", RegexOptions.IgnoreCase);
  71.                 // // 删除href属性中的http链接
  72.                 content = Regex.Replace(content, @"href=""https:\/\/[^""]+", "href="", RegexOptions.IgnoreCase);
  73.                 // //删除style属性中的url里的http链接
  74.                 content = Regex.Replace(content, @"url\(https:\/\/[^)]+\)", "url(#)", RegexOptions.IgnoreCase);
  75.                 // //删除style属性中的url里的http链接
  76.                 content = Regex.Replace(content, @"src=""https:\/\/[^""]+", "src="", RegexOptions.IgnoreCase);
  77.                 // //删除style属性中的url里的http链接
  78.                 content = Regex.Replace(content, @"src=""https:\/\/[^""]+", "src="", RegexOptions.IgnoreCase);
  79.                 content = Regex.Replace(content, @"action=""https:\/\/[^""]+", "action="", RegexOptions.IgnoreCase);
  80.                 content = Regex.Replace(content, @"hm.src=""https:\/\/[^""]+", "hm.src="", RegexOptions.IgnoreCase);
  81.                 File.WriteAllText(file, content);
  82.                 //timer1.Enabled = true;
  83.             }
  84.         }
  85.         public string filename;
  86.         private void timer1_Tick(object sender, EventArgs e)
  87.         {
  88.         }
  89.     }
  90. }
复制代码
源码下载:
https://download.csdn.net/download/weixin_43050480/90140030

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

杀鸡焉用牛刀

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表