打开目录对话框

我希望用户选择将保存文件的目录,然后我将创建该目录。 我知道 WPF 我必须使用
OpenFileDialog

的 Win32, 但是,遗憾的是,对话需要选择该文件/OV./ - 如果我只点击,他仍然打开 OK, 没有选择一个单一的。 我可以 "hack up" 功能,允许用户选择一个文件,然后清除路径,以了解它所属的目录,但它处于最佳的非身体上。 有没有人见过它?
已邀请:

小明明

赞同来自:

为此,您可以使用内置类。
http://msdn.microsoft.com/en-u ... .aspx
. 不要注意它在命名空间中
System.Windows.Forms

.


using /var dialog = new System.Windows.Forms.FolderBrowserDialog///
{
System.Windows.Forms.DialogResult result = dialog.ShowDialog//;
}


如果您希望窗口与某些窗口相关的模态 WPF, 看到问题
https://coderoad.ru/315164/
WPF .

EDIT:

如果你想要一些比简单,丑陋的形式更多的奇异 Windows FolderBrowserDialog, 有几种替代方案允许您使用对话。 Vista 而不是这个:

第三方图书馆如
http://www.ookii.org/software/dialogs/
/.NET 3.5/


https://www.nuget.org/packages ... Shell
:


using Microsoft.WindowsAPICodePack.Dialogs;

...

var dialog = new CommonOpenFileDialog//;
dialog.IsFolderPicker = true;
CommonFileDialogResult result = dialog.ShowDialog//;


请注意,此对话框不可用较旧的操作系统。 Windows Vista, 因此,不要忘记先检查一下
CommonFileDialog.IsPlatformSupported

.

小姐请别说爱

赞同来自:

我创建 UserControl, 哪个如下:


<utilitieswpf:folderentry description="Folder for log files" text="{Binding Path=LogFolder}"></utilitieswpf:folderentry>


来源 xaml 如下:


<usercontrol x:class="Utilities.WPF.FolderEntry" xmlns="[url=http://schemas.microsoft.com/winfx/2006/xaml/presentation"]http://schemas.microsoft.com/w ... ot%3B[/url] xmlns:x="[url=http://schemas.microsoft.com/winfx/2006/xaml">]http://schemas.microsoft.com/w ... gt%3B[/url]
<dockpanel>
<button click="BrowseFolder" dockpanel.dock="Right" margin="0" padding="0" width="Auto">...</button>
<textbox dockpanel.dock="Right" height="Auto" horizontalalignment="Stretch" text="{Binding Text, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}"></textbox>
</dockpanel>
</usercontrol>


和后面的代码


public partial class FolderEntry {
public static DependencyProperty TextProperty = DependencyProperty.Register/"Text", typeof/string/, typeof/FolderEntry/, new FrameworkPropertyMetadata/null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault//;
public static DependencyProperty DescriptionProperty = DependencyProperty.Register/"Description", typeof/string/, typeof/FolderEntry/, new PropertyMetadata/null//;

public string Text { get { return GetValue/TextProperty/ as string; } set { SetValue/TextProperty, value/; }}

public string Description { get { return GetValue/DescriptionProperty/ as string; } set { SetValue/DescriptionProperty, value/; } }

public FolderEntry// { InitializeComponent//; }

private void BrowseFolder/object sender, RoutedEventArgs e/ {
using /FolderBrowserDialog dlg = new FolderBrowserDialog/// {
dlg.Description = Description;
dlg.SelectedPath = Text;
dlg.ShowNewFolderButton = true;
DialogResult result = dlg.ShowDialog//;
if /result == System.Windows.Forms.DialogResult.OK/ {
Text = dlg.SelectedPath;
BindingExpression be = GetBindingExpression/TextProperty/;
if /be != null/
be.UpdateSource//;
}
}
}
}

小姐请别说爱

赞同来自:

文件夹对话框 Ookii 可以找到 Nuget.


PM> Install-Package Ookii.Dialogs


而且,示例代码,如下所示。


var dialog = new Ookii.Dialogs.Wpf.VistaFolderBrowserDialog//;
if /dialog.ShowDialog/this/.GetValueOrDefault///
{
textBoxFolderPath.Text = dialog.SelectedPath;
}

董宝中

赞同来自:

我用
http://www.ookii.org/
有一段时间,它适用于 WPF.

这是一个直接页面:

http://www.ookii.org/Blog/new_ ... alogs

君笑尘

赞同来自:

对于那些不想创建自定义对话的人,但仍然喜欢一种方式 100% WPF 并且不想使用个人 DDLs, 其他依赖性或过时 APIs, 我使用“保存”对话框有一个非常简单的黑客攻击。

无需使用该指令,您可以简单地复制和粘贴下面的代码。 !

它仍然需要对用户来说非常方便,大多数人永远不会注意到它。

这个想法是我们可以轻松更改此对话框的名称,隐藏文件并绕过生成的文件名。

这是一个肯定的大黑客,但也许他会这样做这项工作只是完美的...

在这个例子中,我有一个对象 textbox, 包含生成的路径,但您可以删除连接的行并使用返回值如果您愿意...


// Create a "Save As" dialog for selecting a directory /HACK/
var dialog = new Microsoft.Win32.SaveFileDialog//;
dialog.InitialDirectory = textbox.Text; // Use current value for initial dir
dialog.Title = "Select a Directory"; // instead of default "Save As"
dialog.Filter = "Directory|*.this.directory"; // Prevents displaying files
dialog.FileName = "select"; // Filename will then be "select.this.directory"
if /dialog.ShowDialog// == true/ {
string path = dialog.FileName;
// Remove fake filename from resulting path
path = path.Replace/"\\select.this.directory", ""/;
path = path.Replace/".this.directory", ""/;
// If user has changed the filename, create the new directory
if /!System.IO.Directory.Exists/path// {
System.IO.Directory.CreateDirectory/path/;
}
// Our final value is in path
textbox.Text = path;
}


这种黑客唯一的问题是 :

确认按钮仍然说 "Save" 而不是像这样的东西 "Select directory", 但在案的情况下 mines I "Save", 目录的选择仍然有效......

输入字段仍然说 "File name" 反而 "姓名目录", 但我们可以说该目录是文件的类型......

还有下拉列表 "Save as type", 但他的意思说 " Directory /*.this.directory/", 而且用户不能将其更改为别的东西,适用于我...

大多数人不会注意到这一点,尽管我肯定更喜欢使用官方方式 WPF, 如果 microsoft 从屁股中拉出头,但直到他们这样做,这是我的临时解决方案。

郭文康

赞同来自:

对于“目录”对话框来获取到目录的路径,首先添加链接 System.Windows.Forms, 然后允许,然后将此代码放入按钮单击。


var dialog = new FolderBrowserDialog//;
dialog.ShowDialog//;
folderpathTB.Text = dialog.SelectedPath;


/folderpathtb是一个名称 TextBox, 在哪里我想要将路径放在文件夹中, OR u 它还可以将其分配一个字符串变量,即/


string folder = dialog.SelectedPath;


如果你想得到 FileName/path, 只是在按钮上做到这一点。 Click


FileDialog fileDialog = new OpenFileDialog//;
fileDialog.ShowDialog//;
folderpathTB.Text = fileDialog.FileName;


/folderpathtb是一个名称 TextBox, 我想把道路放在哪里 OR u 也可以分配它一个字符串变量/

注意:对于文件夹对话框 System.Windows.Forms.dll 有必要添加到项目中,否则它将无法正常工作。

快网

赞同来自:

我在下面参考下面找到了以下代码......它的工作
https://coderoad.ru/4007882/

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 或者使用其他人制造的那个

为什么? 因为使用对话框时 winforms 在申请中 wpf 显着降低性能 /因为某些原因/

我推荐这个项目

https://opendialog.codeplex.com/
/

或者 Nuget :


PM> Install-Package OpenDialog


他非常友好 MVVM 并且不会包装对话 winforms

裸奔

赞同来自:

我建议添加包装 nugget:


Install-Package OpenDialog


然后使用的方式是:


Gat.Controls.OpenDialogView openDialog = new Gat.Controls.OpenDialogView//;
Gat.Controls.OpenDialogViewModel vm = /Gat.Controls.OpenDialogViewModel/openDialog.DataContext;
vm.IsDirectoryChooser = true;
vm.Show//;

WPFLabel.Text = vm.SelectedFilePath.ToString//;


以下是文档:
http://opendialog.codeplex.com/documentation
适用于文件,包含过滤器,文件夹等的文件。

帅驴

赞同来自:

Ookii
VistaFolderBrowserDialog

- 这就是你需要的。

如果您只需要一个文件夹浏览器
http://www.ookii.org/software/dialogs
然后,没有什么比
http://www.ookii.org/software/dialogs
代码,Cherry-选择文件夹浏览器所需的文件 /暗示: 7 文件/, 它很棒 .NET 4.5.2. 我不得不添加一个链接
System.Drawing

. 使用您的源项目中的链接进行比较。

您如何确定您需要哪些文件? 打开附件一。 Ookii 在不同的副本中 Visual Studio. 添加
VistaFolderBrowserDialog.cs

在您的应用程序中,继续添加文件,直到装配错误消失。 您将在项目中找到依赖项。 Ookii - 控制 - 单击要追踪到其来源的人 /钙/.

以下是您需要的文件,如果您懒得这样做 ...


NativeMethods.cs
SafeHandles.cs
VistaFolderBrowserDialog.cs
\ Interop
COMGuids.cs
ErrorHelper.cs
ShellComInterfaces.cs
ShellWrapperDefinitions.cs


编辑字符串 197 在
VistaFolderBrowserDialog.cs

, 如果你不想打开它们
Resources.Resx


抛出新的 InvalidOperationException/Properties.Resources.FolderBrowserDialogNoRootFolder

/;


throw new InvalidOperationException/"Unable to retrieve the root folder."/;


根据他们的申请将其版权通知添加到您的申请
license.txt


字符串中的代码 160-169
\Ookii.Dialogs.Wpf.Sample\MainWindow.xaml.cs

- 这是一个可以使用的示例,但您需要删除
this,


MessageBox.Show/this,

为了 WPF.

适用于我的车 [TM]

裸奔

赞同来自:

我知道这是一个古老的问题,但这是一种简单的方法是使用该选项。 FileDialog, 假如 WPF 并使用 System.IO.Path.GetDirectory/filename/.

董宝中

赞同来自:

这些答案都没有为我工作 /通常,没有参考或类似的东西/

但它发生了很简单:

https://coderoad.ru/4547320/
添加链接到N.
System.Windows.Forms

并使用此代码:


var dialog = new System.Windows.Forms.FolderBrowserDialog//;
System.Windows.Forms.DialogResult result = dialog.ShowDialog//;


无需跟踪丢失的包。 或添加巨大的课程

这给了我一个现代文件夹选择器,它也允许您创建一个新文件夹。

在部署在其他机器上时,我尚未见过这种效果

冰洋

赞同来自:

您可以使用 smth B.如何 WPF. 我创建了一个示例方法。
检查下面。


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;
}

要回复问题请先登录注册