private void openfiledialgread_Click(object sender, EventArgs e)
{
openFileDialog1.ShowDialog(); //打开对话框
//定义StreamReader对象实例
StreamReader dataread= new StreamReader(openFileDialog1.FileName, Encoding.Default);
try
{
textBox1.Text = "";
string data= dataread.ReadLine(); //读取打开文件的一行
while (data!= null) //如果打开文件不为空,则一行一行读取
{
textBox1.Text = textBox1.Text + data+ "\r\n";
data= dataread.ReadLine();
}
myfile = openFileDialog1.FileName;
}
catch (Exception mye)
{
MessageBox.Show("读取文件失败!" + data.Message); //提示对话框
}
finally
{
dataread.Close();
}
} |