文件对话框
创建对象
//创建一个FolderBrowserDialog对象
FolderBrowserDialog FBDialog = new FolderBrowserDialog();
属性
| 属性 | 说明 |
|---|---|
| Description | 获取或设置对话框中在树视图控件上显示的说明文本。 |
| RootFolder | 获取或设置从其开始浏览的根文件夹。 |
| SelectedPath | 获取或设置用户选定的路径。 |
| ShowNewFolderButton | 获取或设置一个值,该值指示“新建文件夹”按钮是否显示在文件夹浏览对话框中。 |
description
可以在对话框提供说明、描述、提醒等信息;
FBDialog.Description = "请选择包含img格式的文件夹";
RootFolder
可以设置对话框启动时的根目录,主要为C盘里的文件夹,desktop代表桌面
FBDialog.RootFolder = Environment.SpecialFolder.Desktop;
ShowNewFloderButton
是否在对话框中设置”新建文件夹“按钮
FBDialog.ShowNewFolderButton = true;
SelectedPath
比较重要的属性,它是获取你选择的文件夹的路径,当然我们也可以记住我们每次需要打开的路径,可以避免每次都从桌面开始便利
//开始我们在btn_click()事件外定义一个空的字符串defaultfilePath
if (defaultfilePath != string.Empty)
{
//判断defaultfilePath是否为空,defaultfilePath不为空就可以直接赋值给SelectedPath,每次打开都可以到之前选择的位置
FBDialog.SelectedPath = defaultfilePath;
}
if (FBDialog.ShowDialog() == DialogResult.OK)
{
//空就默认从桌面出发,然后获取点击的路径
defaultfilePath = FBDialog.SelectedPath;
}
方法
| 方法 | 说明 |
|---|---|
| Reset() | 将属性重置为其默认值。 |
例子
定义成员变量:
string defaultfilePath = string.Empty;
文件夹对话框代码:
System.Windows.Forms.FolderBrowserDialog dialog = new System.Windows.Forms.FolderBrowserDialog();
dialog.Description = "从目录添加要搜索的文件:";
dialog.ShowNewFolderButton = false;
dialog.RootFolder = Environment.SpecialFolder.Desktop;
if (defaultfilePath != string.Empty)
{
//判断defaultfilePath是否为空,defaultfilePath不为空就可以直接赋值给SelectedPath,每次打开都可以到之前选择的位置
dialog.SelectedPath = defaultfilePath;
}
System.Windows.Forms.DialogResult result = dialog.ShowDialog();
if (result == DialogResult.OK)
{
//空就默认从桌面出发,然后获取点击的路径
defaultfilePath = dialog.SelectedPath;
}
参考:
https://blog.csdn.net/qq_40581805/article/details/97813943
https://blog.csdn.net/weixin_44448313/article/details/102820439