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

相关推荐
“抚琴”的人16 分钟前
C#中获取程序执行时间
服务器·前端·c#
咚咚咚小柒1 小时前
【前端】用el-popover做通用悬停气泡(可设置弹框宽度)
前端·javascript·vue.js·elementui·html·scss
薄荷撞~可乐2 小时前
C#拆箱/装箱(性能优化)
性能优化·c#
胖咕噜的稞达鸭3 小时前
C++中的父继子承(2)多继承菱形继承问题,多继承指针偏移,继承组合分析+高质量习题扫尾继承多态
c语言·开发语言·数据结构·c++·算法·链表·c#
Hello123网站3 小时前
300多个Html5小游戏列表和下载地址
前端·html·html5
Dm_dotnet4 小时前
WPF应用绑定系统快捷键
c#
.NET修仙日记4 小时前
SQL Server实战指南:从基础CRUD到高并发处理的完整面试题库
面试·职场和发展·c#·.net·sql server·.net全栈经典面试题库
星光一影5 小时前
HIS系统天花板,十大核心模块,门诊/住院/医保全流程打通,医院数字化转型首选
java·spring boot·后端·sql·elementui·html·scss
武文斌776 小时前
项目学习总结:CAN总线、摄像头、STM32概述
linux·arm开发·stm32·单片机·嵌入式硬件·学习·c#
sali-tec6 小时前
C# 基于halcon的视觉工作流-章46-不匀面划痕
人工智能·算法·计算机视觉·c#