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

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

python 复制代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.ProgressBar;

namespace 批量替换文件html
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btn_chose_Click(object sender, EventArgs e)
        {
            string selectedFolderPath = SelectFolder();
            if (!string.IsNullOrEmpty(selectedFolderPath))
            {
                // 用户选择了文件夹,并且可以使用selectedFolderPath
                //MessageBox.Show("选择的文件夹路径: " + selectedFolderPath);
                tbx_LJ.Text = selectedFolderPath;
            }
            else
            {
                MessageBox.Show("没有选择文件夹");
            }
        }

        public static string SelectFolder()
        {
            using (FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog())
            {
                folderBrowserDialog.Description = "请选择一个文件夹";
                DialogResult dialogResult = folderBrowserDialog.ShowDialog();

                if (dialogResult == DialogResult.OK)
                {
                    return folderBrowserDialog.SelectedPath;
                }
                else
                {
                    return string.Empty;
                }
            }
        }
        public int count = 0;
        private void button1_Click(object sender, EventArgs e)
        {
            string folderPath = @tbx_LJ.Text;
            string[] htmlFiles = Directory.GetFiles(folderPath, "*.html", SearchOption.AllDirectories);

            foreach (string file in htmlFiles)
            {

                textBox1.AppendText(file+"\r\n");

                count++;
                lbl_count.Text = count.ToString() + "个";
                Application.DoEvents();
                //timer1.Enabled = true;
                string content = File.ReadAllText(file);
                content = Regex.Replace(content, @"href='https:\/\/[^""]+", "href=\"", RegexOptions.IgnoreCase);
                // // 删除href属性中的http链接
                content = Regex.Replace(content, @"href=""http:\/\/[^""]+", "href=\"", RegexOptions.IgnoreCase);

                // //删除style属性中的url里的http链接
                content = Regex.Replace(content, @"url\(http:\/\/[^)]+\)", "url(#)", RegexOptions.IgnoreCase);

                // //删除style属性中的url里的http链接
                content = Regex.Replace(content, @"src=""http:\/\/[^""]+", "src=\"", RegexOptions.IgnoreCase);

                // // 删除href属性中的http链接
                content = Regex.Replace(content, @"href=""https:\/\/[^""]+", "href=\"", RegexOptions.IgnoreCase);

                // //删除style属性中的url里的http链接
                content = Regex.Replace(content, @"url\(https:\/\/[^)]+\)", "url(#)", RegexOptions.IgnoreCase);

                // //删除style属性中的url里的http链接
                content = Regex.Replace(content, @"src=""https:\/\/[^""]+", "src=\"", RegexOptions.IgnoreCase);

                // //删除style属性中的url里的http链接
                content = Regex.Replace(content, @"src=""https:\/\/[^""]+", "src=\"", RegexOptions.IgnoreCase);

                content = Regex.Replace(content, @"action=""https:\/\/[^""]+", "action=\"", RegexOptions.IgnoreCase);

                content = Regex.Replace(content, @"hm.src=""https:\/\/[^""]+", "hm.src=\"", RegexOptions.IgnoreCase);

                File.WriteAllText(file, content);
                //timer1.Enabled = true;
            }

        }
        public string filename;
        private void timer1_Tick(object sender, EventArgs e)
        {

        }
    }
}

源码下载:

https://download.csdn.net/download/weixin_43050480/90140030

相关推荐
Mr_Xuhhh24 分钟前
NAT、代理服务、内网穿透
网络·网络协议·http·https·udp·智能路由器
与火星的孩子对话1 小时前
Unity高级开发:反射原理深入解析与实践指南 C#
java·unity·c#·游戏引擎·lucene·反射
Dm_dotnet2 小时前
回顾一下WPF原生实现命令
c#
前端老鹰2 小时前
HTML <meta name="color-scheme">:自动适配系统深色 / 浅色模式
前端·css·html
程序设计实验室2 小时前
纯 C#实现+AOT 打造的智能PDF目录提取工具 PdfTocExtractor
c#·aot
BD_Marathon4 小时前
消费者API
c#·linq
全宝5 小时前
🚀前端必学!告别样式冲突:Shadow DOM 终极指南
前端·javascript·html
weisian15117 小时前
HTTP协议-3-HTTP/2是如何维持长连接的?
网络·网络协议·http
CallZhang21018 小时前
Vision Master的C#脚本与opencv联合编程
opencv·计算机视觉·c#·视觉检测
AI视觉网奇18 小时前
kafka 冲突解决 kafka安装
c#·linq