批量查找替换工具(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 - 博客 ...
随机推荐
- markdown表格插入linux变量
一.背景 看标题不难发现这是一个很"小众"的话题,其实本篇是对之前做的单元测试钉钉告警(此篇:https://www.cnblogs.com/ailiailan/p/1322203 ...
- 关于C++当中的“模板函数”
本人C++草鸟,在工作当中遇到了这个问题,就简单做个记录.
- pycharm集成Jupyter Notebook
1. Jupyter Notebook Jupyter项目是一个非盈利的开源项目,源于 2014 年的 ipython 项目,支持运行 40 多种编程语言.Jupyter Notebook 的本质是一 ...
- 终于解决了.net在线客服系统总是被360误报的问题(对软件进行数字签名)
升讯威在线客服与营销系统是基于 .net core / WPF 开发的一款在线客服软件,宗旨是: 开放.开源.共享.努力打造 .net 社区的一款优秀开源产品. 背景 我在业余时间开发的这个客服系统, ...
- C#中 自定义验证规则ValidationAttribute的使用
C#中 自定义验证规则ValidationAttribute的使用 迷恋自留地 进行接口请求的时候难免会对请求字段进行验证,验证对象的所有字段的值是否合乎要求,如进行非空检测,长度检测等等. Requ ...
- [天坑]之qrcode二维码在app内置浏览器中无法显示问题
记录一下最近的工作难点,之一... 首先本项目使用的是qrcode-generator,市面上生成二维码的第三方库有很多qrcode.vue.qrcode.QRious等等 <div id=&q ...
- 论文解读《LightRAG: Simple and Fast Retrieval-Augmented Generation》
博客:https://learnopencv.com/lightrag 视频:https://www.youtube.com/watch?v=oageL-1I0GE 代码:https://github ...
- HBuilderX代码缩进问题
前情 uni-app是我很喜欢的跨平台框架,它能开发小程序,H5,APP(安卓/iOS),对前端开发很友好,自带的IDE让开发体验也很棒,公司项目就是主推uni-app,自然也是用官方自带的IDE了 ...
- 基于.NET8+Vue3开发的权限管理&个人博客系统
前言 今天大姚给大家分享一个基于.NET8+Vue3开发的权限管理&个人博客系统:Easy.Admin. 项目介绍 Easy.Admin是一个基于.NET8+Vue3+TypeScript开发 ...
- Reverse the Rivers 题解
原题链接https://codeforces.com/problemset/problem/2036/E (暂时不会弄翻译,所以不上原题了) 说一下我对题意的理解吧 有n个国家,每个国家有k个区域,用 ...