在winForm中,我们一般会有这样的需求,选择本机的一个图片或者其他文件进去读取或者其他的操作,也可能回选择某一个文件夹下面的所有图片来操作,winForm中为我们提供了两个控件FolderBrowserDialog和OpenFileDialog
FolderBrowserDialog:用来选择一个文件夹,从而读取这个文件夹下面的所有文件
OpenFileDialog:用来读取单个文件
下面来看看他们具体的用法
首先对于这两个控件我们可以从工具箱里托一个过来,也可以直接用代码创建
先看看FolderBrowserDialog的用法,我们拖一个控件到窗体中,然后实现选择,并将路径返回到文本框中

#region 选择pdf文件目录
private void btnBrowse_Click(object sender, EventArgs e)
{
if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
{
txtFile.Text = folderBrowserDialog1.SelectedPath;
}
}
private void btnBrowse_Click(object sender, EventArgs e)
{
if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
{
txtFile.Text = folderBrowserDialog1.SelectedPath;
}
}
OpenFIleDialog的用法也类似

private void button1_Click(object sender, EventArgs e)
{
string Pdfpath = "";
OpenFileDialog op = new OpenFileDialog();
op.Filter = "word Files(*.doc)|*.doc|All Files(*.*)|*.*";
if (op.ShowDialog() == DialogResult.OK)
{
Pdfpath = op.FileName;
}
else
{
Pdfpath = "";
}
textBox1.Text = Pdfpath;
}
{
string Pdfpath = "";
OpenFileDialog op = new OpenFileDialog();
op.Filter = "word Files(*.doc)|*.doc|All Files(*.*)|*.*";
if (op.ShowDialog() == DialogResult.OK)
{
Pdfpath = op.FileName;
}
else
{
Pdfpath = "";
}
textBox1.Text = Pdfpath;
}