-8

Does anyone no a way to replace a word in a txt file with another preset one? maybe using openfile or savefile to find the txt first and then use a text box to determin said word and replace it.

Example using open file to find the txt then typing a word e.g. dog to replace by using a the text box, and all words that have dog in the txt file get changed to cat. Im using visual studios 2012.

if its possible help is much appreciated

swordboot
  • 11
  • 2
  • 3
    Well, you could do this with notepad and use its "find and replace" functionality. – Carra Mar 02 '17 at 08:58
  • 2
    ^ So, yes, it is possible. Start coding. – Patrick Hofman Mar 02 '17 at 08:59
  • 2
    Please refrain from posting questions without any research. A quick google search would provide: http://stackoverflow.com/questions/13509532/how-to-find-and-replace-text-in-a-file-with-c-sharp – Kamil Solecki Mar 02 '17 at 08:59
  • 4
    Possible duplicate of [How to Find And Replace Text In A File With C#](http://stackoverflow.com/questions/13509532/how-to-find-and-replace-text-in-a-file-with-c-sharp) – Kamil Solecki Mar 02 '17 at 08:59

2 Answers2

0

You could just use

string find = "word";
string replace = "replaced";
File.WriteAllText("C:\\yourfile.txt", File.ReadAllText("C:\\yourfile.txt").Replace(find, replace));
Youri Leenman
  • 228
  • 2
  • 8
0

the complete version

private void button1_Click (object sender, EventArgs e)

System.IO.StreamReader sr = new
System.IO.StreamReader(openFileDialog1.FileName);

private void button2_Click (object sender, EventArgs e)

String find textbox1.Text
string replace = "dog";

File.writeAllText(openFileDialog1.FileName,File.ReadAllText(openFileDialog1.FileName).Replace(find, replace));

also need to add a textbox

swordboot
  • 11
  • 2