我想知道是否有人可以帮助我。我希望能够让我的程序让用户能够只从文本文档中读取某个代码块。但是,我希望将它放在按钮后面,以便可以打开和关闭它。我已经尝试过不同的方法来做到这一点,但没有任何改变。
这就是我的代码目前的样子。
namespace filestream
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private string Read(string file)
{
StreamReader reader = new StreamReader(file);
string data = reader.ReadToEnd();
reader.Close();
return data;
}
private void btn1_Click(object sender, EventArgs e)
{
textBox1.Text = "";
DialogResult result = openFileDialog1.ShowDialog();
if (result == DialogResult.OK)
{
string data = Read(openFileDialog1.FileName);
textBox1.Text = data;
}
else
{
//do nothing
}
}
private void button1_Click(object sender, EventArgs e)
{
}
}
}
我对此很陌生,因此将不胜感激任何帮助。