2012-12-21 63 views
0

我写了一个方法,它会遍历所有文本文件,替换文本,并用所述更改更新文本框。它在我第一次运行之后起作用,但随后的执行似乎推断文件没有第一次更改。替换文件中的文本

private void changeText(string searchString, string newString, FileInfo[] listOfFiles) 
{ 
    foreach (FileInfo tempfi in listOfFiles)//Foreach File 
    { 
     string fileToBeEdited = tempfi.FullName; 
     File.SetAttributes(fileToBeEdited, File.GetAttributes(fileToBeEdited) & ~FileAttributes.ReadOnly); //Remove ReadOnly Property 
     string strFile = System.IO.File.ReadAllText(fileToBeEdited); //Reads In Text File 
     if(strFile.Contains(newString))//If the replacement string is contained in the text file 
     { 
      strFile = strFile.Replace(searchString, newString); 
      System.IO.File.WriteAllText(fileToBeEdited, strFile); //Write changes to File 
      myTextBox.Text = "File Changed: " + fileTobeEdited.ToString() + Environment.NewLine; //Notify User 
     } 
    } 
} 

如果我运行这1次或100次我的文本文件更新就好了。如果我第二次运行这个文本框,它会重新更新,说它更新了新文件。

我希望这种方法在第一次运行后不会找到任何要替换的文本。

+3

它不会出现您实际上替换文件中的任何内容。所以,你实际上每次都找到相同的内容。 – PhoenixReborn

+1

您不会对文件进行任何更改。您将其数据读取到strFile,检查strFile是否包含一些字符串,将srtFile保存回文件。没有编辑 – Steve

+0

对不起(这是在我的代码中,但没有在我的初始文章 – Demasterpl

回答

1

变量fileToBeEdited未初始化。

你必须查找包含searchString而非newString的文件!

private void changeText(string searchString, string newString, FileInfo[] listOfFiles) 
{ 
    foreach (FileInfo tempfi in listOfFiles) { 
     string fileToBeEdited = tempfi.FullName; // <== This line was missing 
     File.SetAttributes(tempfi.FullName, File.GetAttributes(fileToBeEdited) & 
              ~FileAttributes.ReadOnly); 
     string strFile = System.IO.File.ReadAllText(fileToBeEdited); 
     if (strFile.Contains(searchString)) { // <== replaced newString by searchString 
      strFile = strFile.Replace(searchString, newString); 
      System.IO.File.WriteAllText(fileToBeEdited, strFile); 
      myTextBox.Text = "File Changed: " + fileToBeEdited.ToString() + 
          Environment.NewLine; 
     } 
    } 
} 
+0

是的,我在第一篇文章中修复了“fileToBeEdited”部分。 Contains参数。 – Demasterpl

0

它看起来并不像你实际上正在改变文件。您正在检查文件中是否包含字符串,如果是,则将该文件写回。你将不得不做这样的事情:

private void changeText(string searchString, string newString, FileInfo[] listOfFiles) 
{ 
    foreach (FileInfo tempfi in listOfFiles)//Foreach File 
    { 
     File.SetAttributes(fileToBeEdited, File.GetAttributes(fileToBeEdited) & ~FileAttributes.ReadOnly); //Remove ReadOnly Property 
     string strFile = System.IO.File.ReadAllText(fileToBeEdited); //Reads In Text File 
     if(strFile.Contains(newString))//If the replacement string is contained in the text file 
     { 
      strFile = strFile.Replace(searchString,newString); // make the changes 
      System.IO.File.WriteAllText(fileToBeEdited, strFile); //Write changes to File 
      myTextBox.Text = "File Changed: " + fileTobeEdited.ToString() + Environment.NewLine; //Notify User 
     } 
    } 
} 

然后你就可以真正将更改保存到文件,并在第一次运行之后的新文件将被写入。

+0

对不起,这是我的错,我错过了“fileToBeEditied.Replace”在我的第一篇文章中写了一篇文章: – Demasterpl

0

也许我误解了代码,但是您似乎错过了替换!

string strFile = System.IO.File.ReadAllText(fileToBeEdited); //Reads In Text File 
    if(strFile.Contains(searchString))//If the replacement string is contained in the text file 
    { 
     strFile = strFile.Replace(searchString, newString); 
.... 

另请注意我如何检查文件是否包含搜索字符串,而不是新闻串。

+0

是的,谢谢,我错过了我在我的文章中解决了这个问题,但是在我的代码中。 – Demasterpl