在form1的顶部我做了
OpenFiledialog openFileDialog1 = new OpenFiledialog();
Run Code Online (Sandbox Code Playgroud)
然后:
private void changeWorkingDirectoryToolStripMenuItem_Click(object sender, EventArgs e)
{
DialogResult result = openFileDialog1.ShowDialog();
openFileDialog1.Filter =
"BMP|*.bmp|GIF|*.gif|JPG|*.jpg;*.jpeg|PNG|*.png|TIFF|*.tif;*.tiff|"
+ "All Graphics Types|*.bmp;*.jpg;*.jpeg;*.png;*.tif;*.tiff";
openFileDialog1.InitialDirectory = @"c:\";
openFileDialog1.Multiselect = true;
if (result == DialogResult.OK)
{
string[] files = openFileDialog1.FileNames;
try
{
if (files.Length > 0)
{
label6.Text = files.Length.ToString();
label6.Visible = true;
string directoryPath = Path.GetDirectoryName(files[0]);
label12.Text = directoryPath;
label12.Visible = true;
}
}
catch (IOException)
{
}
}
}
Run Code Online (Sandbox Code Playgroud)
但是当我点击按钮时,init目录是文件,而不是c:我根本看不到过滤器,但我看到了所有文件.这就像我没有影响的设置.
ShowDialog 是模态的,所以它等待用户单击确定/取消.
只有这样,其余的代码才会运行.您应该只ShowDialog在完成属性设置后调用:
openFileDialog1.Filter = "BMP|*.bmp|GIF|*.gif|JPG|*.jpg...";
openFileDialog1.InitialDirectory = @"c:\";
openFileDialog1.Multiselect = true;
DialogResult result = openFileDialog1.ShowDialog();
if (result == DialogResult.OK)
Run Code Online (Sandbox Code Playgroud)