少し調べてみましたが、質問に対する答えが見つかりませんでした。
用語を検索して置換するためのこのコードがあります。
ポイントは、私のアプリケーションでは、いくつかのファイルとフォルダーのパスを選択し、そのパスを .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=
て変更すると、最も古い値が「oldhost」であると想定されます。したがって、すべての行を削除してから、その行に書き込む必要があります。"db.host=" + tbhost.Text
db.host=myhostoldhost
tbhost.Text=myhost
db.host=