C#初学者的问题,有一点不明白??

来源:百度知道 编辑:UC知道 时间:2024/06/22 13:26:07
有2句话不明白,加了注释了,希望详细解答一下,多谢了

namespace WindowsApplication27
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public string fileName;
private void Form1_Load(object sender, EventArgs e)
{

}

private void button1_Click(object sender, EventArgs e)
{
this.openFileDialog1.ShowDialog();//这句话是什么意思?
fileName = this.openFileDialog1.FileName;//不明白??
string str = "";
StreamReader s = new StreamReader(fileName, Encoding.GetEncoding("gb2312"));
//while ((st = s.ReadLine()) != null)
//{
// str += st;
//}
str = s.ReadToEnd();
s.Close();
this.richTextBox1.Text = str;

单击button1按钮
this.openFileDialog1.ShowDialog();//弹出选择文件对话框
fileName = this.openFileDialog1.FileName;//在对话框中选择文件名

这段代码意思是读取文件

this.openFileDialog1.ShowDialog();//这句话是什么意思?
fileName = this.openFileDialog1.FileName;//不明白??
其中 第一句的意思是 打开文件选则对话框
第二句的意思是 fileName是你选中的文件的名字

this.openFileDialog1.ShowDialog();//直接弹出文件选择框
fileName = this.openFileDialog1.FileName;//把选择的文件路径存放到全局变量fileName中!
最好这样写:
if(this.openFileDialog1.ShowDialog()==DialogResult.OK)
{
fileName=this.openFileDialog1.FileName;
//如果不判断的话可能没有选择还是继续给变量赋值和读取,会错误滴!
}