我的下面的程序是一个简单的窗体,用于搜索目录中的文件,然后打开,读取和写入文件,之后有一个搜索按钮,该文件,但我只能做到这一点与扩展名为.txt的文件可以一些帮助我我也想这样做的文档,以及如果该文件是另一个扩展我想打开文件.txt和.doc扩展名我要弹出一个错误,它无法打开该文件,这是我下面的代码,是有任何人谁可以帮我修改这个程序或者给我出出主意使用扩展名为.txt和.doc的文件
namespace my_project
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog of = new OpenFileDialog();
of.ShowDialog();
textBox1.Text = of.FileName;
}
private void button2_Click(object sender, EventArgs e)
{
StreamReader sr = new StreamReader(textBox1.Text);
richTextBox1.Text = sr.ReadToEnd();
sr.Close();
}
private void button3_Click(object sender, EventArgs e)
{
StreamWriter sw = new StreamWriter(textBox1.Text, true);
sw.WriteLine(textBox2.Text);
sw.Close();
}
private void button4_Click(object sender, EventArgs e)
{
int index = 0; string temp = richTextBox1.Text; richTextBox1.Text = ""; richTextBox1.Text = temp;
while (index < richTextBox1.Text.LastIndexOf(textBox3.Text))
{
richTextBox1.Find(textBox3.Text, index, richTextBox1.TextLength, RichTextBoxFinds.None);
richTextBox1.SelectionBackColor = Color.Yellow;
index = richTextBox1.Text.IndexOf(textBox3.Text, index) + index;
}
}
}
}