Dee*_*Raj -1 c# string list console-application
我有两个文件,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)
}
}
Run Code Online (Sandbox Code Playgroud)
您可以使用String.Replace。示例代码:
string text = File.ReadAllText("file1.txt");
text = text.Replace("text needs to be replaced", "new text");
File.WriteAllText("file1.txt", text);
Run Code Online (Sandbox Code Playgroud)