c#2.0开发的一个文本字符串替换工具,控制台工具,可以批量替换

简介:

using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;

namespace replace
{

class Program
{
    static void Main(string[] args)
    {

        if (args.Length == 0||(args.Length == 1 && (args[0] == "/?" || args[0] == "?" || args[0] == "-?")))
        {
            Console.WriteLine("replace 命令行工具");
            Console.WriteLine("作者:zj53hao@qq.com");
            Console.WriteLine("CSDN:http://blog.csdn.net/zj53hao");
            Console.WriteLine("用途:在很多需要动态更改某些软件的配置文件时非常有用");
            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine("replace 命令运用语法阐明");
            Console.WriteLine("  replace [/?|?|-?]    显示帮助信息");
            Console.WriteLine("  replace [path/]filename search replace [/option]  交换文本文件中的字符串,支持通配符文件名");
            Console.WriteLine("  replace setname search replace [/option]  交换参数变量中的字符串");
            Console.WriteLine("/option参数阐明:");
            Console.WriteLine("   i 区分大小写.  默许不区分");
            Console.WriteLine("   r 运用正则表达式查找文本。留空则运用纯文本交换");
            Console.WriteLine("   f 交换文件 默许");
            Console.WriteLine("   v 交换的是系统环境变量.和f不能同时出现");
            Console.WriteLine();
            Console.WriteLine("如: replace *.txt 查找字符串 交换字符串");
            Console.WriteLine("如: replace setname 查找字符串 交换字符串");
            Console.WriteLine("如: replace myinc/*.txt 查找字符串 交换字符串 /i");
            Console.WriteLine("如: replace setname 查找字符串 交换字符串 /irv");
        }
        if (args.Length == 3)
        {
            ReplaceFiles(args[0],args[1],args[2],null);
        }

        if (args.Length == 4)
        {
            if (args[3].Contains("v"))
            {
                ReplaceVariable(args[0], args[1], args[2], args[3]);
            }
            else
            {
                ReplaceFiles(args[0], args[1], args[2], args[3]);
            }
        }

    }

    /// 
    /// 交换环境变量中某个变量的文本值。可以是系统变量,用户变量,临时变量。交换时会覆盖原始值。小心运用
    /// 
    /// 
    /// 
    /// 
    /// 
    public static void ReplaceVariable(string variable, string search, string replace, string options)
    {
        string text=Environment.GetEnvironmentVariable(variable);
        System.Windows.Forms.MessageBox.Show(text);
        text=ReplaceText(text, search, replace, options);
        Environment.SetEnvironmentVariable(variable, text);
        text = Environment.GetEnvironmentVariable(variable);
        System.Windows.Forms.MessageBox.Show(text);
    }
    /// 
    /// 批量交换文件文本
    /// 
    /// 
    public static void ReplaceFiles(string path,string search, string replace, string options)
    {
        
        string[] fs;
        if(File.Exists(path)){
            ReplaceFile(path, search, replace, options);
            return;
        }
        if (Directory.Exists(path))
        {
            fs = Directory.GetFiles(path);
            foreach (string f in fs)
            {

                ReplaceFile(f, search, replace, options);
            }
            return;
        }

        int i=path.LastIndexOf("/");
        if(i<0)i=path.LastIndexOf("/");
        string d, searchfile;
        if (i > -1)
        {
            d = path.Substring(0, i + 1);
            searchfile = path.Substring(d.Length);
        }
        else
        {
            d = System.Environment.CurrentDirectory;
            searchfile = path;
        }

        searchfile = searchfile.Replace(".", @".");
        searchfile = searchfile.Replace("?", @"[^.]?");
        searchfile = searchfile.Replace("*", @"[^.]*");
        //System.Windows.Forms.MessageBox.Show(d);  System.Windows.Forms.MessageBox.Show(searchfile);
        if (!Directory.Exists(d)) return;
        fs = Directory.GetFiles(d);
        foreach (string f in fs)
        {
            if(System.Text.RegularExpressions.Regex.IsMatch(f,searchfile))
                ReplaceFile(f, search, replace, options);
        }
    }
    
    /// 
    /// 交换单个文本文件中的文本
    /// 
    /// 
    /// 
    /// 
    /// 
    /// 
    public static bool ReplaceFile(string filename, string search, string replace,string options)
    {
        FileStream fs = File.OpenRead(filename);
        

        //判别文件是文本文件还二进制文件。该方法似乎不科学
        byte b;
        for (long i = 0; i < fs.Length; i++)
        {
            b = (byte)fs.ReadByte();
            if (b == 0)
            {
                System.Windows.Forms.MessageBox.Show("非文本文件");
                return false;//有此字节则表示改文件不是文本文件。就不必交换了
            }
        }

        //判别文本文件编码规则。
        byte[] bytes = new byte[2];
        Encoding coding=Encoding.Default;
        if (fs.Read(bytes, 0, 2) > 2)
        {
            if (bytes == new byte[2] { 0xFF, 0xFE }) coding = Encoding.Unicode;
            if (bytes == new byte[2] { 0xFE, 0xFF }) coding = Encoding.BigEndianUnicode;
            if (bytes == new byte[2] { 0xEF, 0xBB }) coding = Encoding.UTF8;
        }

        fs.Close();

        //交换数据
        string text=File.ReadAllText(filename, coding);
        text=ReplaceText(text,search, replace, options);
        File.WriteAllText(filename, text, coding);
        return true;
    }
    /// 
    /// 交换文本
    /// 
    /// 
    /// 
    /// 
    /// 
    /// 
    public static string ReplaceText(string text, string search, string replace, string options)
    {

        RegexOptions ops = RegexOptions.None;
        if (options == null)  //纯文本交换
        {
            search = search.Replace(".", @".");
            search = search.Replace("?", @"?");
            search = search.Replace("*", @"*");
            search = search.Replace("(", @"(");
            search = search.Replace(")", @")");
            search = search.Replace("[", @"[");
            search = search.Replace("[", @"[");
            search = search.Replace("[", @"[");
            search = search.Replace("{", @"{");
            search = search.Replace("}", @"}");
            ops |= RegexOptions.IgnoreCase;
        }
        else
        {
            if(options.Contains("I"))ops |= RegexOptions.IgnoreCase;
        }

        text = Regex.Replace(text, search, replace, ops);
        return text;
    }
}

}

本文转自博客园知识天地的博客,原文链接:c#2.0开发的一个文本字符串替换工具,控制台工具,可以批量替换 转载请自行联系原博主。

相关文章
|
9天前
|
开发框架 前端开发 .NET
LIMS(实验室)信息管理系统源码、有哪些应用领域?采用C# ASP.NET dotnet 3.5 开发的一套实验室信息系统源码
集成于VS 2019,EXT.NET前端和ASP.NET后端,搭配MSSQL 2018数据库。系统覆盖样品管理、数据分析、报表和项目管理等实验室全流程。应用广泛,包括生产质检(如石化、制药)、环保监测、试验研究等领域。随着技术发展,现代LIMS还融合了临床、电子实验室笔记本和SaaS等功能,以满足复杂多样的实验室管理需求。
20 3
LIMS(实验室)信息管理系统源码、有哪些应用领域?采用C# ASP.NET dotnet 3.5 开发的一套实验室信息系统源码
|
2天前
|
前端开发 测试技术 C#
如何开发一套基于C#和.NET 6.0手术麻醉系统? 手术麻醉系统源码
如何开发一套基于C#和.NET 6.0手术麻醉系统?
10 1
|
19天前
|
算法 C#
C#开源实用的工具类库,集成超过1000多种扩展方法
C#开源实用的工具类库,集成超过1000多种扩展方法
|
27天前
|
开发框架 .NET C#
使用C#进行.NET框架开发:深入探索与实战
【5月更文挑战第28天】本文探讨了C#在.NET框架中的应用,展示了其作为强大编程语言的特性,如类型安全、面向对象编程。C#与.NET框架的结合,提供了一站式的开发环境,支持跨平台应用。文中介绍了C#的基础知识,如数据类型、控制结构和面向对象编程,以及.NET的关键技术,包括LINQ、ASP.NET和WPF。通过一个实战案例,展示了如何使用C#和ASP.NET开发Web应用,包括项目创建、数据库设计、模型和控制器编写,以及视图和路由配置。本文旨在揭示C#在.NET开发中的深度和广度,激发开发者探索更多可能性。
|
1月前
|
XML 监控 Dubbo
Dubbo03【管理控制台和监控中心搭建】,Java开发实用必备的几款插件
Dubbo03【管理控制台和监控中心搭建】,Java开发实用必备的几款插件
|
19天前
|
监控 网络协议 C#
一款基于C#开发的通讯调试工具(支持Modbus RTU、MQTT调试)
一款基于C#开发的通讯调试工具(支持Modbus RTU、MQTT调试)
|
1月前
|
测试技术 持续交付 C#
C#程序基础开发入门学习笔记
C#是一种现代的、面向对象的编程语言,广泛应用于Windows应用程序开发、游戏开发(尤其是Unity引擎)、Web应用程序以及跨平台应用等。
34 0
|
1月前
|
监控 安全 C#
开发公司电脑监控软件的报警系统:一个C#示例
在当今数字化时代,企业对其计算机网络和系统的安全性和稳定性越来越重视。为了确保员工遵守公司政策、保护机密信息以及监控系统的正常运行,开发一种可靠的公司电脑监控软件变得至关重要。本文将介绍如何使用C#编写一个简单而有效的报警系统,以便监控关键数据并在必要时发出警报。
99 0
|
1月前
|
Java
java实战项目超市管理系统控制台版
java实战项目超市管理系统控制台版
|
1月前
|
Java
【Java开发指南 | 第二十篇】Java流之控制台
【Java开发指南 | 第二十篇】Java流之控制台
22 2