自己写了了个批量查找替换工具(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. Python 中的抽象类和接口类

    [抽象类] 抽象类是一个特殊的类,只能被继承,不能被实例化.它主要用于作为其他类的基类或模板. 抽象类可以包含抽象方法和具体方法.在抽象类中定义的抽象方法必须在子类中进行实现. from abc im ...

  2. C++ STL 容器简介

    1.总述 C++ STL(Standard Template Library)是 C++ 标准库的一部分,包括了许多数据结构的实现,提供了许多好用的轮子,同时,其设计思想也非常值得学习.其中,容器是 ...

  3. 2.9 使用系统光盘修复Linux系统

    如果系统错误已经导致单用户模式不能进入了,那么是否需要重新安装Linux 系统?不用着急,为了应对单用户模式也无法修复的错误,Linux 系统提供了 rescue 救援(光盘修复)模式. rescue ...

  4. app&小程序&web安全—sign签名绕过

    零.前言 在web界面登陆时,小程序和app传输数据时经常会碰到签名,会对请求的参数进行签名,如果自己修改了数据包就会校验失败非常麻烦. 本文编写的契机就是因为碰到了一个JeecgBoot的小程序, ...

  5. 2013年ImportNew最受欢迎的10篇文章

    2013年即将过去,提前祝大家元旦快乐,ImportNew 整理出了本年度最受欢迎的前10篇Java和Android技术文章,每篇文章仅添加了摘要.如果您是我们的新访客,那下面这些文章不能错过.如果您 ...

  6. Liunx-Shell脚本

    shell可以理解为对命令行的一个解释器,命令行输入命令,shell执行,linux系统输出结果 1. shell脚本格式 开头: #!/bin/bash #!告诉系统其后路径所指定的程序即是解释此脚 ...

  7. 鸿蒙NEXT元服务:论如何免费快速上架作品

    [引言]天下武功,唯快不破. 本文讨论如何免费且以最快速度上架自己的作品. 作者以自己从零开始到提交发布审核一共俩小时的操作流程分享给大家作参考. [1]立项选择 结论:元服务,单机,工具类(非游戏) ...

  8. 一个关于CountDownLatch的并发需求

    需求 A,B,C可并发运行,全部成功才算成功,一个失败全员回滚. 思考 使用CountDownLatch,可以保证三个线程结束后,才进行提交成功状态.但是怎么才能判断某个任务失败了呢? 捕获子线程异常 ...

  9. PLC编程—数据类型

    CPU 型号不同,实际的有效数据类型与文中略有不同(大同小异) 数据类型概述 基本数据类型(二进制数.整数.浮点数.定时器.DATE.TOD.LTOD.CHAR.WCHAR) 复杂数据类型(DT.LD ...

  10. [NET,C# ] Nuget包发布流程

    1.新建一个.NET Core类库 2.新增一个方法,并编译项目 3.下载Nuget.exe,与刚才新建的类库放在同一目录下 下载地址:https://www.nuget.org/downloads ...