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

发布于:2024-12-21 ⋅ 阅读:(11) ⋅ 点赞:(0)

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

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