我希望用户选择一个目录,我将生成的文件将保存在其中。我知道在WPF中,我应该使用Win32中的OpenFileDialog,但不幸的是,对话框需要选择文件-如果我只是单击确定而不选择一个,它就会保持打开。我可以通过让用户选择一个文件,然后剥离路径以找出它属于哪个目录来“hack”该功能,但这充其量是不直观的。有人见过这种情况吗?
当前回答
对于目录对话框获取目录路径,首先添加引用System.Windows。表单,然后解析,然后把这个代码放在按钮点击。
var dialog = new FolderBrowserDialog();
dialog.ShowDialog();
folderpathTB.Text = dialog.SelectedPath;
(folderpathTB是文本框的名字,我想把文件夹路径,或者你可以把它分配给一个字符串变量也就是)
string folder = dialog.SelectedPath;
如果你想获得文件名/路径,只需在按钮单击上这样做
FileDialog fileDialog = new OpenFileDialog();
fileDialog.ShowDialog();
folderpathTB.Text = fileDialog.FileName;
(folderpathTB是文本框的名字,我想把文件路径,或者你可以把它分配给一个字符串变量太)
注意:对于文件夹对话框,必须将System.Windows.Forms.dll添加到项目中,否则它将无法工作。
其他回答
Ookii对话框包括一个选择文件夹(而不是文件)的对话框:
https://github.com/ookii-dialogs
看来微软。Win32 . net库不支持选择文件夹(只支持文件),所以你在WPF中运气不好(截至2022年7月)。我觉得现在最好的选择是WPF的Ookii: https://github.com/ookii-dialogs/ookii-dialogs-wpf。它工作得很好,正如预期的那样,在WPF减去微软的支持。你可以把它作为NuGet包来获取。XAML视图背后的代码:
public partial class ExportRegionView : UserControl
{
public ExportRegionView()
{
InitializeComponent();
}
private void SavePath(object sender, RoutedEventArgs e)
{
var dialog = new Ookii.Dialogs.Wpf.VistaFolderBrowserDialog();
dialog.Description = "SIPAS Export Folder";
dialog.UseDescriptionForTitle = true;
if (dialog.ShowDialog().GetValueOrDefault())
{
ExportPath.Text = dialog.SelectedPath;
}
}
}
XAML: <Button Grid.Row="1" Grid.Column="3" Style="{DynamicResource Esri_Button}" Click="SavePath" Margin="5,5,5,5">Path</Button>
你可以像这样在WPF中使用smth。我已经创建了示例方法。 下面的检查。
public string getFolderPath()
{
// Create OpenFileDialog
Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Multiselect = false;
openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
if (openFileDialog.ShowDialog() == true)
{
System.IO.FileInfo fInfo = new System.IO.FileInfo(openFileDialog.FileName);
return fInfo.DirectoryName;
}
return null;
}
我在下面的链接上找到了下面的代码…这个方法奏效了 选择文件夹对话框WPF
using Microsoft.WindowsAPICodePack.Dialogs;
var dlg = new CommonOpenFileDialog();
dlg.Title = "My Title";
dlg.IsFolderPicker = true;
dlg.InitialDirectory = currentDirectory;
dlg.AddToMostRecentlyUsedList = false;
dlg.AllowNonFileSystemItems = false;
dlg.DefaultDirectory = currentDirectory;
dlg.EnsureFileExists = true;
dlg.EnsurePathExists = true;
dlg.EnsureReadOnly = false;
dlg.EnsureValidNames = true;
dlg.Multiselect = false;
dlg.ShowPlacesList = true;
if (dlg.ShowDialog() == CommonFileDialogResult.Ok)
{
var folder = dlg.FileName;
// Do something with selected folder string
}
我知道这是一个老问题,但一个简单的方法是使用WPF提供的FileDialog选项,并使用System.IO.Path.GetDirectory(filename)。
推荐文章
- 如何将枚举绑定到WPF中的组合框控件?
- 我如何腾出一个StackPanel的子元素?
- Linux删除大小为0的文件
- WPF和初始焦点
- 如何确定在android文件的MIME类型?
- WPF数据网格底部空行
- 窗口vs页面vs用户控件的WPF导航?
- 在窗口上设置设计时间DataContext是一个编译器错误?
- 调用线程必须是STA,因为许多UI组件都要求这一点
- ContentControl和ContentPresenter有什么区别?
- WPF中控制模板和数据模板的区别
- 如何隐藏关闭按钮在WPF窗口?
- UI线程上的任务继续
- WPF数据绑定:我如何访问“父”数据上下文?
- 从System.Drawing.Bitmap中加载WPF BitmapImage