批量查找替换工具(C#)
自己写了了个批量查找替换工具(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#)的更多相关文章
- sublime text3怎么批量查找替换文件夹中的字符
在编写代码的时候,往往有些代码是重复的,但是如果要改一处代码,其他的地方也要改.那么怎么批量修改呢?下面小编就以sublime text3为例来讲解一下sublime text3怎么批量查找替换文件夹 ...
- linux下批量查找/替换文本内容
一般在本地电脑上批量替换文本有许多工具可以做到,比如sublime text ,但大多服务器上都是无图形界面的,为此收集了几条针对linux命令行 实现批量替换文本内容的命令: 1.批量查找某个目下文 ...
- pycharm批量查找替换,正则匹配
ctrl + r:查找替换 ctrl+f:查找 ctrl+shift+r:全局查找替换 ctrl+alt+f:全局查找 shift+tab将代码左对齐 replace all 完成
- sublime text3 批量查找替换文件夹或项目中的字符
1.点击左上角的“菜单”,在弹出的菜单中选择“打开文件夹”. 2.在文件夹上右击,选择“在文件夹中查找”选项 3.之后会软件底部会弹出对话框,分别输入要查找的内容和替换的内容,最后点击替换按钮 4.再 ...
- str_replace 批量查找替换字符串
<?php $str = 'I Love You!'; $str = str_replace('o','O',$str,$count); echo $str.PHP_EOL; // I LOve ...
- Linux 批量查找替换方法(VIM和sed)
版权声明:欢迎与我交流讨论,若要转载请注明出处~ https://blog.csdn.net/sinat_36053757/article/details/70946263 1.VIM命令 当前行进行 ...
- LittleTools之批量替换工具
身为程序员,有很多事情都可以交给机器来做,这样可以提高工作效率. 在此先写个批量替换工具,用来将某些对象统一替换为另一对象. 比方说场景中摆了一堆树,位置.比例.旋转都已经调好了,但是对树的样式不太满 ...
- webstorm批量查找,批量替换快捷键
ctrl+shift+f:批量查找,我的webstorm11不能用ctrl+shift+f进行批量查找了,不知道什么原因,自己又胡乱实验了一下, 发现ctrl+shift+g+f可以批量查找 ctrl ...
- Linux 批量查找并替换文件夹下所有文件的内容
1.批量查找某个目下文件的包含的内容 cd etc grep -rn "查找的内容" ./ 2.批量替换某个目下所有包含的文件的内容 cd etc sed -i "s/查 ...
- PyCharm中批量查找及替换
选中需要操作的字符 Ctrl + R 替换 Ctrl + Shift + F 全局查找 Ctrl + Shift + R 全局替换 源自: PyCharm中批量查找及替换 - Ella_Wu - 博客 ...
随机推荐
- 一文彻底弄懂Spring IOC 依赖注入
Spring IOC(Inversion of Control,控制反转)依赖注入是 Spring 框架的核心特性之一,旨在实现对象之间的松耦合,提升代码的可维护性.可测试性和可扩展性.下面我们将从以 ...
- C++ 函数模板与类模板
目录 16.1.1 函数模板 16.1.2 类模板 定义类模板 实例化模板 在类外定义成员函数 类模板成员函数的实例化 类模板和友元 模板类型别名 类模板参数的static成员 16.1.3 模板参数 ...
- NZOJ 模拟赛4
T1 数字游戏 大家列队后,都觉得累了,于是一起坐到院子中的草地上休息.这时Anna突然想跟她的最大竞争对手Cici玩一个数字游戏,她要你编写程序帮助她取得胜利. 第i次游戏初始时有一个整数N_i(1 ...
- golang 正则表达式
package main import "bytes" import "fmt" import "regexp" func main() { ...
- Java线程中断的本质和编程原则
在历史上,Java试图提供过抢占式限制中断,但问题多多,例如前文介绍的已被废弃的Thread.stop.Thread.suspend和 Thread.resume等.另一方面,出于Java应用代码的健 ...
- mysql5.7之密码重置
一.windows下更改mysql数据库密码在windows下找到my.ini文件,例如:C:\ProgramData\MySQL\MySQL Server 5.7,打开该文件夹下的my.ini文件, ...
- Educational Codeforces Round 155 (Rated for Div
B. Chips on the Board 题解:贪心 显然我们可以把题意转化为:对于任意一个\((i,j)\),我们可以花费\(a_{i,j}\)的代价占据第\(i\)行和第\(j\)列,求占据所有 ...
- 推荐一款强大的开源物联网 Web 组态软件
前言 快速发展的物联网(IoT)领域,设备管理和监控的需求日益增长.为了满足这一需求并提供更高效的解决方案. 向大家推荐一款强大的开源物联网Web组态软件.这款软件不仅具备灵活的可视化配置功能,还提供 ...
- MySQL 时区与 serverTimezone
TL;DR 手动为 MySQL 指定非偏移量的时区,以避免 TIMESTAMP 类型夏令时问题和时区转化性能瓶颈 TIMESTAMP 范围:'1970-01-01 00:00:01' UTC to ' ...
- 在分布式追踪系统中使用 W3C Trace Context
在分布式追踪系统中使用 W3C Trace Context https://dev.to/luizhlelis/using-w3c-trace-context-standard-in-distribu ...