窗体拖拽一个button,代码样例中使用FolderBrowserDialog对象new一个新实例,等同于在窗体拖拽FolderBrowserDialog控件,两种方式都可以,实现功能效果是相同的
using System;
using System.Windows.Forms;
namespace WindowsFormsApp11
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();
folderBrowserDialog.Description = "请选择文件路径";
//// 设置根目录在桌面
//folderBrowserDialog.RootFolder = Environment.SpecialFolder.Desktop;
// 设置当前选择的路径, folderBrowserDialog.RootFolder 两种方式选择其一即可
folderBrowserDialog.SelectedPath = Application.StartupPath;
// 允许在对话框中包括一个新建目录的按钮
folderBrowserDialog.ShowNewFolderButton = true;
if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
{
// 获取选择目录
string path = folderBrowserDialog.SelectedPath;
MessageBox.Show(path);
// 业务操作...
// 业务操作...
// 业务操作...
}
}
}
}