自己写了了个批量查找替换工具(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. 你还用ES存请求日志?ClickHouse+Vector打造最强Grafana日志分析看板

    为什么要做NGINX日志分析看板 Grafana官网的dashboards有NGINX日志采集到ES数据源的展示看板,也有采集到LOKI数据源的展示看板,唯独没有采集到ClickHouse数据源的展示 ...

  2. [Apache Doris] Apache Doris 架构及代码目录解读

    一.系统架构 Doris是一个MPP的OLAP系统,主要整合了Google Mesa(数据模型),Apache Impala(MPP Query Engine)和Apache ORCFile (存储格 ...

  3. cmu15545笔记-Join算法(Join Algorithms)

    目录 Overview Nested Loop Join Naïve Block Index Sort-Merge Join Hash Join Simple Hash Join Partition ...

  4. VMware安装教程---------------------以及Windows,Linux,Apple MAC OS系统安装

    1.什么是VMware虚拟机 VMware虚拟机是一个虚拟机软件,它可以在一台机器上同时运行多个系统,这些系统包括Windows,Linux,Apple os等. 2.虚拟机有什么用 虚拟机的用处很多 ...

  5. 如何挑选海外4G模组?这里有秘籍!

    今天我会告诉大家如何挑选海外4G模组,我会把优势给贴出作为参考.去过国外的都知道国外4G网络各种状况实在让人无力吐槽,做海外设备的朋友,是时候了解一下Air780EEN/EEU/EEJ系列海外模组-- ...

  6. QT5.15.2 连接MySQL 驱动问题解决方案,无论菜鸟🐦️还是老鸟🦜,解决了就是好鸟🦚

    最新在学QT,现在QT只能在线安装了,用了几天,看到数据库时,需要用MySQL,结果出现了问题. QSqlDatabase: QMYSQL driver not loaded. QSqlDatabas ...

  7. Solr学习总结-Facet

    返回查询集合中指定field的统计情况,例如找到city一样的文档数目: 加入文档 <add> <doc> <field name="id">1 ...

  8. FreeRTOS-Error: ..\FreeRTOS\portable\RVDS\ARM_CM4F\port.c, 271

    任务运行时间API函数的应用 当使用任务运行时间API函数打印任务运行时间等相关信息是,串口打印出现这个问题 网上是这么说的 但是运行每个任务都可以执行进去,循序时间API函数也可以执行进去,就是无法 ...

  9. python爬虫利器之Playwright

    Playwright 是微软在 2020 年初开源的新一代自动化测试工具,它的功能类似于 Selenium.Pyppeteer 等,都可以驱动浏览器进行各种自动化操作.它的功能也非常强大,对市面上的主 ...

  10. highcharts在vue中的应用

    1.安装命令 npm install highcharts --save 2.在页面中按需引入 import Highcharts from 'highcharts/highstock'; impor ...