马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
x
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
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |