- 窗体布局
- 代码
private void btnSelectPath_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog2 = new OpenFileDialog();
openFileDialog2.Multiselect = true; //该值确定是否可以选择多个文件
openFileDialog2.Title = "请选择文件";
openFileDialog2.Filter = "图片(*.jpg,*.gif,*.bmp)|*.jpg;*.gif;*.bmp";
openFileDialog2.Filter = "文档(*.doc;*.docx)|*.doc;*.docx";
if (openFileDialog2.ShowDialog() == DialogResult.OK)
{
if (openFileDialog2.FileName != "")
{
this.textBox1.Text = openFileDialog2.FileName;
}
}
}
Filter 属性 赋值为一字符串 用于过滤文件类型;
字符串说明如下:
‘|’分割的两个,一个是注释,一个是真的Filter,显示出来的是那个注释。如果要一次显示多中类型的文件,用分号分开。
如:
Open1.Filter=“图片文件(.jpg,.gif,.bmp)|.jpg;.gif;.bmp”;
则过滤的文件类型为 “|”号 右边的 .jpg;.gif;.bmp 三种类型文件,在OpenDialog/SaveDialog中显示给用户看的文件类型字符串
则是 :“|”号 左边的 图片文件(.jpg,.gif,.bmp)。
ps:选择文件左上角的图标由窗体的图标决定