我需要使用 C# 将具有特定字符串的行替换为另一个文件的内容

发布于 2025-01-12 09:24:14 字数 1242 浏览 2 评论 0原文

我有两个文件,File1.txt 和 File2.txt,我需要使用 C# 控制台应用程序将 file1 中的特定字符串替换为 file2 的内容。

文件 1:

这是一个示例文件

..

代码块 ..

文件的一些文本

结尾

文件 2 :

// 一些 c# 代码 //

我需要用文件 2 的内容替换字符串“code-block”。

编辑 1:

我尝试阅读文件既可以是数组,也可以是列表

var fileContent = File.ReadAllLines(file_path);
            List<string> allLinesText = File.ReadAllLines(file_path).ToList();
            string parentdirectory = Directory.GetParent(Path.GetDirectoryName(file_path)).FullName.ToString(); 

            foreach( var line in fileContent)
            {

                if ( line.Contains("{% code-block %}"))
                {
                    string code_path = line.Replace("{%", string.Empty).Replace("%}", string.Empty).Trim();
                    string code_fullpath = Path.Combine(parentdirectory, code_path);
                    if(File.Exists(code_fullpath))
                    {
                        var code_content = File.ReadAllLines(code_fullpath);
                       // int insert_code_at = 

                     // allLinesText.Insert( allLinesText.IndexOf(line)
                    }
                    
                }

I have two files , File1.txt and File2.txt , I need to replace a particular string in file1 with contents of file2 using C# console application.

file 1 :

This is a sample file

..

code-block
..

some text

end of file

File 2 :

// some c# code //

i need to replace the string "code-block" with the contents of File 2.

Edit 1:

I had tried to read the file as Array and also as a list

var fileContent = File.ReadAllLines(file_path);
            List<string> allLinesText = File.ReadAllLines(file_path).ToList();
            string parentdirectory = Directory.GetParent(Path.GetDirectoryName(file_path)).FullName.ToString(); 

            foreach( var line in fileContent)
            {

                if ( line.Contains("{% code-block %}"))
                {
                    string code_path = line.Replace("{%", string.Empty).Replace("%}", string.Empty).Trim();
                    string code_fullpath = Path.Combine(parentdirectory, code_path);
                    if(File.Exists(code_fullpath))
                    {
                        var code_content = File.ReadAllLines(code_fullpath);
                       // int insert_code_at = 

                     // allLinesText.Insert( allLinesText.IndexOf(line)
                    }
                    
                }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

为你鎻心 2025-01-19 09:24:14

您可以使用String.Replace。示例代码:

string text = File.ReadAllText("file1.txt");
text = text.Replace("text needs to be replaced", "new text");
File.WriteAllText("file1.txt", text);

You can use String.Replace. Sample code:

string text = File.ReadAllText("file1.txt");
text = text.Replace("text needs to be replaced", "new text");
File.WriteAllText("file1.txt", text);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文