【问题标题】:Read File, Search for Term and replace all line of this term读取文件,搜索词条并替换该词条的所有行
【发布时间】: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


    【解决方案1】:

    在写入文件之前使用以下代码

    Regex r = new Regex(@"\s");
    String[] tokens = r.Split(tempLineValue);
    string settings = string.Empty;
    for (int i = 0; i < tokens.Length; i++)
    {
       if (tokens[i].Contains(searchTerm))
       {
           settings = tokens[i];
           break;
       }
    }
    if (settings != string.Empty)
        outputWriter.WriteLine((tempLineValue.Replace(settings, replaceTerm));
    else
        outputWriter.WriteLine(tempLineValue);
    

    假设:你的主人只有一个词。

    【讨论】:

      【解决方案2】:

      usercr 我有这样的代码:

      using (FileStream inputStream = File.OpenRead(originalFile))
              {
                  using (StreamReader inputReader = new StreamReader(inputStream))
                  {
                      using (StreamWriter outputWriter = File.AppendText(outputFile))
                      {
                          while (null != (tempLineValue = inputReader.ReadLine()))
                          {
      
                                                          Regex r = new Regex(@"\s");
                              String[] tokens = r.Split(tempLineValue);
                              string settings = string.Empty;
                              for (int i = 0; i < tokens.Length; i++)
                              {
                                 if (tokens[i].Contains(searchTerm))
                                 {
                                     settings = tokens[i];
                                     break;
                                 }
                              }
                              outputWriter.WriteLine((tempLineValue.Replace(settings, replaceTerm)));
                          }
                      }
                  }
              }
      

      我得到了异常“字符串不能为零长度。参数名称:oldvalue”

      【讨论】:

      • 我已经在上面的代码中修复了。需要检查是否有任何行有db.host配置,可以通过检查设置是否为空来完成。
      • 感谢 usercr,我正在工作中这样做,现在我在家,所以明天我会尝试代码。
      猜你喜欢
      • 2018-09-20
      • 1970-01-01
      • 1970-01-01
      • 2020-07-29
      • 2018-08-10
      • 2019-06-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多