在单击事件中搜索单词中的多个文件,然后将其替换为单个单词



我需要在文件中搜索日志文件中的单词,例如 "INFO""ERROR""FATAL",并且需要将其替换为复选框上的 "DEBUG"单词。如果我取消选中框,则"DEBUG"的单词应替换为原始状态,例如"INFO""ERROR"E.T.C。

我尝试使用File.Replace()方法。但这对于搜索&仅替换一个单词。我不确定如何处理它的数组或其他东西。另外,如何保存文件的先前状态。

我使用了以下代码。但是在某个地方,我正在犯错。

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 .NETGPM_Service.exe.config", text);
}

我不明白为什么这个问题会受到负面票,这是一个有趣的问题,因为它提出了一个概念,该概念对具有相同问题或相似问题的未来读者有用。

@priya,我创建了此方法,该方法替换了您指定的所有单词

在此示例中,更换了带有"FUNNY""FATAL", "ERROR", "WARN"

string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + @"Test.txt";
PriyaMethod(path, true, "FUNNY", "FATAL", "ERROR", "WARN");

另外,如您所见,我使用string保存文件内容的先前状态,需要恢复时要调用。


        using System.IO;
        //Save the original content to restore it when required
        private string SavedState = String.Empty;
        private void PriyaMethod(string FilePath, bool IsDebug, string ReplaceWord, params string[] WordsToReplace)
        {
            //string[] WordsToUse = { "INFO", "ERROR", "FATAL", "DEBUG", "WARN" };
            if (System.IO.File.Exists(FilePath))
            {
                // string[] Lines = System.IO.File.ReadAllLines(FilePath);
                // string[] Words = System.IO.File.ReadAllText(FilePath).Split(' 
                 string Text = System.IO.File.ReadAllText(FilePath);
                // byte[] Bytes = System.IO.File.ReadAllBytes(FilePath);
                // IEnumerable<string> ReadLines = System.IO.File.ReadLines(FilePath);*/
                 SavedState = Text;
                 if (IsDebug)
                 {
                    for (int i = 0; i < WordsToReplace.Length; ++i)
                    {
                        Text = Text.Replace(WordsToReplace[i], ReplaceWord);
                    }
                    //If the file is open, it closes it to prevent an IOException from occurring
                    File.Open(FilePath, FileMode.Open, FileAccess.Write, FileShare.Read).Close();
                    File.WriteAllText(FilePath, Text); //Write new content
                 }
                 else
                 {
                    //If the file is open, it closes it to prevent an IOException from occurring
                    File.Open(FilePath, FileMode.Open, FileAccess.Write, FileShare.Read).Close();
                    File.WriteAllText(FilePath, SavedState); //Restore old content
                 }
            }
            else
            {
                throw new FileNotFoundException(System.String.Format("The file {0} does not exists", FilePath));
            }
        }

如果您要跳过线:

string[] Lines = System.IO.File.ReadAllLines(FilePath);
int LinePosition = 0; // First line
Lines[LinePosition] = String.Empty // Delete selected line

string Content = System.IO.File.ReadAllText(FilePath);
string[] Lines = Content.Split('n');
int LinePosition = 1; // Second line
Lines[LinePosition] = null // Delete selected line

组装新奇:

string NewContent = String.Empty; // Content without selected line
// Assembly new content
foreach(string Line in Lines) NewContent += Line + "n";
//Or
// Assembly new content
foreach(string Line in Lines) NewContent += String.Format("{0}{1}", Line, "n");

无法评论,所以写答案。您的其他陈述是错误的:

else if (text.Contains("DEBUG"))

应该是:

else if (text.Contains("INFO"))

我假设您的文本不包含"WARN""INFO"。如果是这样,previousState事情将无法工作。

最新更新