I am trying to write a program that can replace text and replace also Regex text. So I am having trouble with the regex replace part..I'm a real noob :)
private void button2_Click(object sender, EventArgs e)
{
if (File.Exists(textBox1.Text))
{
//this is the regular replace:
if (checkBox1.Checked == false)
{
StreamReader sr = new StreamReader(textBox1.Text);
StreamWriter sw = new StreamWriter(textBox1.Text.Replace(".", "_new."));
string cur = "";
do
{
cur = sr.ReadLine();
cur = cur.Replace(textBox2.Text, textBox3.Text);
sw.WriteLine(cur);
}
while (!sr.EndOfStream);
sw.Close();
sr.Close();
MessageBox.Show("Finished, the new file is in the same directory as the old one");
}
//this is the REGEX replace:
if (checkBox1.Checked == true)
{
System.Text.RegularExpressions.Regex g = new Regex(@textBox2.Text);
using (StreamReader r = new StreamReader(textBox1.Text))
{
StreamReader sr = new StreamReader(textBox1.Text);
StreamWriter sw = new StreamWriter(textBox1.Text.Replace(".", "_new."));
string cur = "";
do
{
cur = sr.ReadLine();
cur = cur.Replace(textBox2.Text, textBox3.Text);
sw.WriteLine(cur);
}
while (!sr.EndOfStream);
sw.Close();
sr.Close();
}
MessageBox.Show("Finished, the new file is in the same directory as the old one");
}
button2.Enabled = false;
}
if (File.Exists(textBox1.Text) == false)
{
MessageBox.Show("Please select a file and try again.");
}
}