【发布时间】:2013-09-24 21:50:31
【问题描述】:
我做了一些研究,但找不到我的问题的答案。
我有这个代码来搜索一个词并替换它。
关键是,在我的应用程序中,我想选择一些文件和文件夹路径,然后将该路径写入我的 .properties 文件,以便“值”始终在变化。
在我的.properties 文件中,我有db.host=localhost,我想将其更改为用户选择的主机名,例如db.host=myhost。这是我的代码:
string originalFile = @"C:\wamp\bin\php\php5.4.3\build.properties";
string outputFile = @"C:\wamp\bin\php\php5.4.3\build1.properties";
string searchTerm = "db.host=";
string replaceTerm="db.host=" + tbhost.Text ;
string tempLineValue;
using (FileStream inputStream = File.OpenRead(originalFile))
{
using (StreamReader inputReader = new StreamReader(inputStream))
{
using (StreamWriter outputWriter = File.AppendText(outputFile))
{
while (null != (tempLineValue = inputReader.ReadLine()))
{
outputWriter.WriteLine(tempLineValue.Replace(searchTerm, replaceTerm));
}
}
}
}
所以现在如果我搜索db.host= 并将其更改为"db.host=" + tbhost.Text,我的文件中的结果是db.host=myhostoldhost,假设我的tbhost.Text=myhost 并且最旧的值是“oldhost”。所以我需要找到db.host=删除所有行,然后写入该行。
【问题讨论】:
标签: c# winforms replace streamwriter