如何完全替换以特定单词开头的特定文本行 [英] How to replace a particular text line completely which starts with a specific word

查看:83
本文介绍了如何完全替换以特定单词开头的特定文本行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文本文件。我的要求是用输入完全替换特定的行。我的问题是只有一半的线是恒定的,剩下的一半可以是任何东西。







到目前为止,我已经尝试了以下



I have a text file. My requirement is to replace the particular line completely with the input. My problem is only half of that line is constant and the remaining half can be anything.



So far I have tried the below

static void Main(string[] args)
        {
            //1.Copy the file from one location to another location
            string sourcePath = @"D:\\SQLConfigurationFile\\ConfigurationFile.ini";
            string destinationPath = @"C:\\Temporary\\ConfigurationFile.ini";
            File.Copy(sourcePath, destinationPath);
            //--End 1--//
                        
            string productKey = "Ali";
            string userName = "Mujtaba";

            string filePath = @"C:\\Temporary\\ConfigurationFile.ini";
            var text = new StringBuilder();
            var text2 = new StringBuilder();

            foreach (string s in File.ReadAllLines(filePath))
            {
                text.AppendLine(s.Replace("PID=ProductKey", "PID=" + "\"" + productKey + "\""));
            }

            using (var file = new StreamWriter(File.Create(filePath)))
            {
                file.Write(text.ToString());
            }

            foreach (string s in File.ReadAllLines(filePath))
            {
                text2.AppendLine(s.Replace("SQLSYSADMINACCOUNTS=User", "SQLSYSADMINACCOUNTS=" + "\"" + userName + "\""));
            }

            using (var file = new StreamWriter(File.Create(filePath)))
            {                
                file.Write(text2.ToString());
            }
            

        }





我的尝试:



例如:

PID = ProductKey



这里PID是常量,ProductKey可以是任何东西。现在我有硬编码的ProductKey,并尝试了下面的代码正在工作。

但是当ProductKey改变时,我的代码将无效。



请帮助!!



What I have tried:

For instance:
" PID=ProductKey"

Here PID is constant and ProductKey can be anything. For now I have hard coded ProductKey and tried the below code which is working.
But when the ProductKey changes my code won't work.

Please Help!!

推荐答案

我建​​议从这里开始:代码项目知识库 [ ^ ]



您需要使用ini阅读器/解析器/编写器,例如:

INI文件 [ ^ ]

Ini Handler [ ^ ]

简化的INI处理 [ ^ ]
I'd suggest to start here: Code Project Knowledge Base[^]

You need to use ini reader/parser/writer, for example:
INI Files[^]
Ini Handler[^]
Simplified INI Handling[^]


您应该了解正则表达式(RegEx)



这是一个链接到RegEx文档:

perlre - perldoc.perl.org [ ^ ]
You should learn about Regular Expressions (RegEx)

Here is a link to RegEx documentation:
perlre - perldoc.perl.org[^]


最后完成..



Finally Done..

string filePath = @"C:\\Temporary\\ConfigurationFile.ini";
            
            string[] arrLine = File.ReadAllLines(filePath);
            for (int i = 0; i < arrLine.Count(); i++)
            {
                if (arrLine[i].Contains("PID"))
                {
                    arrLine[i] = "PID=" + "\"" + productKey + "\"";
                }
                if (arrLine[i].Contains("SQLSYSADMINACCOUNTS"))
                {
                    arrLine[i] = "SQLSYSADMINACCOUNTS=" + "\"" + userName + "\"";
                }
            }
            File.WriteAllLines(filePath, arrLine);


这篇关于如何完全替换以特定单词开头的特定文本行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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