自己写了了个批量查找替换工具(C#),目前已知问题有查找速度不够快,假死现象等。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
/*
author:wgscd
date:2020-11-30
email:wgscd@126.com
*/
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using System.Windows.Forms;
using System.Diagnostics; namespace ADFindAndReplace
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DATA_GRID.DataContext = resultList;
} bool isReplace = false;
bool includeSubFolder = false;
string searchPattern = "*.*";
string replaceWords = "";
ObservableCollection<Result> resultList = new ObservableCollection<Result>();
private void btnSearch_Click(object sender, RoutedEventArgs e)
{
isReplace = false; doWork(); }
private void btnReplace_Click(object sender, RoutedEventArgs e)
{
isReplace = true;
replaceWords = txtReplace.Text;
doWork();
} void doWork()
{
if (txtPath.Text.Trim() == "" || txtWords.Text == "" || txtFileType.Text.Trim() == "")
{
System.Windows.MessageBox.Show("please fill setting");
return;
}
if (isReplace)
{
if (txtReplace.Text == "")
{ return;
} } includeSubFolder = (bool)ckSubfolder.IsChecked;
searchPattern = txtFileType.Text;
resultList.Clear(); string path = txtPath.Text.Trim();
string words = txtWords.Text;
try
{ getFiles(path, words);
}
catch (Exception ex)
{ System.Windows.MessageBox.Show(ex.Message);
} }
void getFiles(string path, string words)
{ string[] fileList = new string[] { };
if (includeSubFolder)
{
fileList = Directory.GetFiles(path, searchPattern, SearchOption.AllDirectories);
}
else
{
fileList = Directory.GetFiles(path, searchPattern);
}
foreach (string f in fileList)
{ try
{
var str = File.ReadAllText(f);
if (str.Contains(words))
{
int i = str.IndexOf(words);
resultList.Add(new Result() { FilePath = f, Briefwords = "" + str.PadLeft(8).PadRight(8).Substring(i - 8, 8 + words.Length) });
if (isReplace)
{
File.WriteAllText(f, str.Replace(words, replaceWords));
}
}
}
catch (Exception ex)
{ Debug.Print(ex.Message);
} } } private void btnSelectFolder_Click(object sender, RoutedEventArgs e)
{
FolderBrowserDialog df = new FolderBrowserDialog();
//df.Description = "select path";
df.ShowNewFolderButton = false;
DialogResult result = df.ShowDialog();
if (result == System.Windows.Forms.DialogResult.OK)
{
txtPath.Text = df.SelectedPath;
} }
}
public class Result : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private string filePath { get; set; }
private string briefwords { get; set; }
private int lineNumber { get; set; } public string FilePath
{ get
{
return filePath; } set
{
filePath = value;
NotifyChanged("FilePath");
} }
public string Briefwords
{ get
{
return briefwords; } set
{
briefwords = value;
NotifyChanged("Briefwords");
} }
public int LineNumber
{ get
{
return lineNumber; } set
{
lineNumber = value;
NotifyChanged("LineNumber");
} }
void NotifyChanged(string propName)
{ if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propName));
}
}
}
}

  

<Window x:Class="ADFindAndReplace.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:ADFindAndReplace"
mc:Ignorable="d"
Title="ADFindAndReplace" Height="450" Width="883">
<Grid>
<Label HorizontalAlignment="Left" Height="23" Margin="0,21,0,0" Content="serach words:" VerticalAlignment="Top" Width="90"/> <TextBox x:Name="txtWords" HorizontalAlignment="Left" Height="23" Margin="113,21,0,0" TextWrapping="Wrap" Text="wgscd" VerticalAlignment="Top" Width="551"/>
<Label HorizontalAlignment="Left" Height="23" Margin="10,51,0,0" Content="serach Path:" VerticalAlignment="Top" Width="90"/> <TextBox x:Name="txtPath" HorizontalAlignment="Left" Height="23" Margin="113,51,0,0" TextWrapping="Wrap" Text="c:\" VerticalAlignment="Top" Width="200"/>
<Button x:Name="btnSelectFolder" HorizontalAlignment="Left" Height="21" Margin="318,53,0,0" Content="...Bowser" VerticalAlignment="Top" Width="60" Click="btnSelectFolder_Click"/> <CheckBox x:Name="ckSubfolder" VerticalContentAlignment="Center" HorizontalAlignment="Left" Height="23" Margin="392,51,0,0" Content="include sub folders" VerticalAlignment="Top" Width="134"/> <Label HorizontalAlignment="Left" Height="25" Margin="531,53,0,0" Content="File Type:" VerticalAlignment="Top" Width="60"/> <TextBox x:Name="txtFileType" ToolTip="*.*;*.txt;*.doc etc.." Text="*.*" HorizontalAlignment="Left" Height="23" Margin="611,55,0,0" VerticalAlignment="Top" Width="53"> </TextBox>
<Label HorizontalAlignment="Left" Height="23" Margin="10,88,0,0" Content="Replace Words:" VerticalAlignment="Top" Width="90"/>
<TextBox x:Name="txtReplace" HorizontalAlignment="Left" Height="23" Margin="113,88,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="225"/> <Button x:Name="btnSearch" Content="Search" HorizontalAlignment="Left" Margin="688,13,0,0" VerticalAlignment="Top" Width="104" Height="42" Click="btnSearch_Click"/>
<Button x:Name="btnReplace" Content="Replace" HorizontalAlignment="Left" Margin="688,73,0,0" VerticalAlignment="Top" Width="104" Height="38" Click="btnReplace_Click"/> <DataGrid x:Name="DATA_GRID" ItemsSource="{Binding}" AutoGenerateColumns="False" Margin="0,128,0,34">
<DataGrid.Columns>
<DataGridTextColumn Header="file" Binding="{Binding FilePath}" MinWidth="422"/>
<DataGridTextColumn Header="Briefwords" Binding="{Binding Briefwords}"/>
</DataGrid.Columns>
</DataGrid>
<Label Name="lbState" Height="29" Content="Ready" VerticalAlignment="Bottom"/>
</Grid>
</Window>

  

批量查找替换工具(C#)的更多相关文章

  1. sublime text3怎么批量查找替换文件夹中的字符

    在编写代码的时候,往往有些代码是重复的,但是如果要改一处代码,其他的地方也要改.那么怎么批量修改呢?下面小编就以sublime text3为例来讲解一下sublime text3怎么批量查找替换文件夹 ...

  2. linux下批量查找/替换文本内容

    一般在本地电脑上批量替换文本有许多工具可以做到,比如sublime text ,但大多服务器上都是无图形界面的,为此收集了几条针对linux命令行 实现批量替换文本内容的命令: 1.批量查找某个目下文 ...

  3. pycharm批量查找替换,正则匹配

    ctrl + r:查找替换 ctrl+f:查找 ctrl+shift+r:全局查找替换 ctrl+alt+f:全局查找 shift+tab将代码左对齐 replace all 完成

  4. sublime text3 批量查找替换文件夹或项目中的字符

    1.点击左上角的“菜单”,在弹出的菜单中选择“打开文件夹”. 2.在文件夹上右击,选择“在文件夹中查找”选项 3.之后会软件底部会弹出对话框,分别输入要查找的内容和替换的内容,最后点击替换按钮 4.再 ...

  5. str_replace 批量查找替换字符串

    <?php $str = 'I Love You!'; $str = str_replace('o','O',$str,$count); echo $str.PHP_EOL; // I LOve ...

  6. Linux 批量查找替换方法(VIM和sed)

    版权声明:欢迎与我交流讨论,若要转载请注明出处~ https://blog.csdn.net/sinat_36053757/article/details/70946263 1.VIM命令 当前行进行 ...

  7. LittleTools之批量替换工具

    身为程序员,有很多事情都可以交给机器来做,这样可以提高工作效率. 在此先写个批量替换工具,用来将某些对象统一替换为另一对象. 比方说场景中摆了一堆树,位置.比例.旋转都已经调好了,但是对树的样式不太满 ...

  8. webstorm批量查找,批量替换快捷键

    ctrl+shift+f:批量查找,我的webstorm11不能用ctrl+shift+f进行批量查找了,不知道什么原因,自己又胡乱实验了一下, 发现ctrl+shift+g+f可以批量查找 ctrl ...

  9. Linux 批量查找并替换文件夹下所有文件的内容

    1.批量查找某个目下文件的包含的内容 cd etc grep -rn "查找的内容" ./ 2.批量替换某个目下所有包含的文件的内容 cd etc sed -i "s/查 ...

  10. PyCharm中批量查找及替换

    选中需要操作的字符 Ctrl + R 替换 Ctrl + Shift + F 全局查找 Ctrl + Shift + R 全局替换 源自: PyCharm中批量查找及替换 - Ella_Wu - 博客 ...

随机推荐

  1. Nginx 安全配置

    server { listen 8089; server_name 10.5.210.203:8089; #charset koi8-r; #access_log logs/host.access.l ...

  2. 整理程序员面试中HR最常问的问题

    前言 不要慌,不要怕,要调适好自己的情绪,在介绍自己的基本情况时面无表情,说的头头是道,必要的时候包装下自己,不属于你的功能也能说是你的(难不成会去查?查也查不到).不过在面试前,建议可以找自己的朋友 ...

  3. 5.Kubeadm和二进制方式对比

    Kubeadm方式搭建K8S集群 安装虚拟机,在虚拟机安装Linux操作系统[3台虚拟机] 对操作系统初始化操作 所有节点安装Docker.kubeadm.kubelet.kubectl[包含mast ...

  4. C#中的9个“黑魔法”

    C#中的9个"黑魔法"与"骚操作" 我们知道C#是非常先进的语言,因为是它很有远见的"语法糖".这些"语法糖"有时过于好 ...

  5. OpenCompass使用LawBench数据测评本地Qwen大模型

    一.思维导图展示 二.OpenCompass简介 OpenCompass是一个大模型测评体系,开源.高效.同时集成CompassKit测评工具.CompassHub测评集社区,CompassRank测 ...

  6. 如何制作一个HTML页面的锁屏功能

    如果后台一些界面比较敏感,希望主动或者被动的在人员不想暴露信息的情况下加一把锁,就是说避免信息一直在页面上暴露,可以使用"阅后即焚"这种思路,这种思路比较简单,显示了就过几秒删除, ...

  7. after_request 可以直接用于接口日志

  8. Python:pygame游戏编程之旅七(pygame基础知识讲解1)

    与Python自带的random.math.time等模块一样,Pygame框架也带有许多模块来提供绘图.播放声音.处理鼠标输入等功能. 本章将讲述Pygame提供的基本模块及功能,并假设读者已经具有 ...

  9. 分布式对象存储之FDFS

    1.它是一个开源的分布式文件系统,它对文件进行管理. 功能有:文件存储.文件同步.文件访问(文件的上传下载)等.特别适合以文件为主的在线服务. 2.fastDFS服务端有两个角色:跟踪器(tracke ...

  10. 如何优雅地在Django项目里生成不重复的ID?

    前言 本来标题是想叫"生成不重复的四位数"的,不过单纯数字有点局限,推广一下变成不重复 ID 吧~ 这个功能是在做下面图片里这个小项目时遇到的,有点像微信的面对面建群,生成一个随机 ...