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

相关推荐
一天八小时2 小时前
计算机网络————(一)HTTP讲解
网络协议·计算机网络·http
软件黑马王子5 小时前
C#初级教程(4)——流程控制:从基础到实践
开发语言·c#
曦月合一6 小时前
html中iframe标签 隐藏滚动条
前端·html·iframe
水煮庄周鱼鱼10 小时前
C# 入门简介
开发语言·c#
软件黑马王子10 小时前
Unity游戏制作中的C#基础(6)方法和类的知识点深度剖析
开发语言·游戏·unity·c#
Nicole Potter11 小时前
请说明C#中的List是如何扩容的?
开发语言·面试·c#
gu2013 小时前
c#编程:学习Linq,重几个简单示例开始
开发语言·学习·c#·linq
m0_5287238113 小时前
HTML中,title和h1标签的区别是什么?
前端·html
Dark_programmer13 小时前
html - - - - - modal弹窗出现时,页面怎么能限制滚动
前端·html
哑巴语天雨14 小时前
前端面试-网络协议篇
websocket·网络协议·http·面试·https