OpenFileDialog的效果如图:
1 //一般都是点击一个按钮然后打开 2 private void btnSelectFile1_Click(object sender, EventArgs e) 3 { 4 //默认路径,注意这里写路径时要用c:\\而不是c:\ 5 openFileDialog1.InitialDirectory = "c:\\"; 6 //限定打开文件的格式 7 openFileDialog1.Filter = "文本文件|*.*|C#文件|*.cs|所有文件|*.*"; 8 openFileDialog1.RestoreDirectory = true; 9 openFileDialog1.FilterIndex = 1; 10 //这句话照写就行了,意思是选中文件后,点击open后要做的事情 11 if (openFileDialog1.ShowDialog() == DialogResult.OK) 12 { 13 //把选中的文件的路径在label控件上显示 14 lblFile1.Text = openFileDialog1.FileName; 15 } 16 }