搜索单词中的多个文件,并将其替换为单击事件上的单个单词 [英] Search multiple files in a word and replace it to single word on a click event

查看:90
本文介绍了搜索单词中的多个文件,并将其替换为单击事件上的单个单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在文件中搜索INFO,ERROR,WARN,FATAL等日志文件中的单词,并且需要将其替换为单击复选框上的DEBUG字样。如果我取消选中此框,DEBUG中的单词应替换为INFO,ERROR等原始状态





我不知道如何处理数组或其他东西。另外,如何保持文件的先前状态?



I need to search the words in a log file like "INFO", "ERROR", "WARN" "FATAL" in a file and need to replace it to the word "DEBUG" on the check box click. If I uncheck the box, the words from "DEBUG" should replaced to the original state like "INFO", "ERROR" e.t.c.


I am not sure how to handle it Array or something. Also, how to hold the previous state of the file?

private void ToggleforDebugMode(bool IsDebug)
    {
        string text = @"C:\Program Files (x86)\Hi.config";
        string filePath = Path.GetDirectoryName("Hi.config") + fileName;
        text = File.ReadAllText(@"C:\Program Files (x86)\Hi.config");
        if (IsDebug)
        {
            _previousState = text.Contains("WARN") ? "WARN" : "INFO";

            if(text.Contains("WARN"))
            {
                text = text.Replace("WARN", "DEBUG");
            }
            else if (text.Contains("DEBUG"))
            {
                text = text.Replace("INFO", "DEBUG");
            }               

        }
        else
        {
             text = text.Replace("DEBUG", _previousState);
        }
        File.WriteAllText(@"C:\Program Files (x86)\Galileo Print Manager .NET\GPM_Service.exe.config", text);
    }





我的尝试:



我试图使用File.Replace()方法。但它有助于搜索&只替换一个单词。



What I have tried:

I tried to use File.Replace() method. But it is helpful for searching & replacing only one word.

推荐答案

替换单词的原因是什么,为什么不简单地使用多个替换操作。例如

What comes to replacing the words, why not simply use multiple Replace operations. For example
...
text = text.Replace("INFO", "DEBUG");
text = text.Replace("ERROR", "DEBUG");
text = text.Replace("WARN", "DEBUG");
...





这是前一个状态,可能最简单的方法是重新读取文件到变量。



And that comes to the previous state, probably the easiest way is to re-read the file to the variable.


这篇关于搜索单词中的多个文件,并将其替换为单击事件上的单个单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆