我需要做以下事情:
更改文本文件中的行
[Path] = "c:\this\certain\path\"
Run Code Online (Sandbox Code Playgroud)
用这条线
[Path] = "c:\that\other\newer\path\"
Run Code Online (Sandbox Code Playgroud)
这些路径肯定会有不同的长度,所以我需要更换引号中的内容或完全删除行并输入一个新的,但是在同一个地方,不要附加到文档的末尾.
这样就可以了
Dim thefile As String = "filepath"
Dim lines() As String = System.IO.File.ReadAllLines("filepath")
lines(number of line you want to replace) = "write what you want to replace here"
System.IO.File.WriteAllLines(filepath, lines)
Run Code Online (Sandbox Code Playgroud)
将文本文件读入字符串,迭代每一行并检查它是否采用以下格式:
[Path] = "...."
(使用正则表达式或简单地使用string.StartsWith("[Path] = ")
)
在此循环中,您应该写出所有其他行,当您位于此 [Path] 行时,打印出修改后的行。
所以在代码中(抱歉,是用 C# 编写的):
var reader = File.OpenText("foo.txt");
var writer = new StreamWriter("output.txt");
string line;
while ((line=reader.ReadLine()) != null)
{
if (line.StartsWith("[Path]"))
writer.WriteLine("[Path] = \"c:\\that\\other\\newer\\path\\\"");
else
writer.WriteLine(line);
}
Run Code Online (Sandbox Code Playgroud)
当然,关闭并处置StreamReader和StreamWriter。
归档时间: |
|
查看次数: |
41180 次 |
最近记录: |