方法一:曲线救国,用 OpenFileDialog
OpenFileDialog openFileDialog = new OpenFileDialog
{
Multiselect = false,
Title = "选择新建项目的位置",
CheckFileExists = false,
CheckPathExists = true,
FileName = "Folder Selection"
};
if (openFileDialog.ShowDialog() == true)
{
...
}
方法二:使用 WinForms
中的 FolderBrowserDialog
更改项目设置:在 usewpf
下面增加:
<UseWindowsForms>true</UseWindowsForms>
using System;
using System.Windows.Forms;
...
using var dialog = new FolderBrowserDialog
{
Description = "Time to select a folder",
UseDescriptionForTitle = true,
SelectedPath = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory)
+ Path.DirectorySeparatorChar,
ShowNewFolderButton = true
};
if (dialog.ShowDialog() == DialogResult.OK)
{
...
}