【发布时间】:2008-09-17 07:33:18
【问题描述】:
在文件路径字段上,我想捕获目录路径,例如:
textbox1.Text = directory path
有人吗?
【问题讨论】:
标签: c# directory openfiledialog
在文件路径字段上,我想捕获目录路径,例如:
textbox1.Text = directory path
有人吗?
【问题讨论】:
标签: c# directory openfiledialog
如果您希望用户选择文件夹,可以使用 FolderBrowserDialog 类。
http://msdn.microsoft.com/en-us/library/system.windows.forms.folderbrowserdialog.aspx
DialogResult result = folderBrowserDialog1.ShowDialog();
if (result.Equals(get_DialogResult().OK)) {
textbox1.Text = folderBrowserDialog1.get_SelectedPath();
}
如果您只想从完整路径获取目录,您可以这样做:
textbox1.Text = Path.GetDirectoryName(@"c:\windows\temp\myfile.txt");
这会将 Text-property 设置为 "c:\windows\temp\"
【讨论】:
我正在使用 VS 2008 SP1。这就是我所需要的:
private void button1_Click(object sender, EventArgs e)
{
FolderBrowserDialog profilePath = new FolderBrowserDialog();
if (profilePath.ShowDialog() == DialogResult.OK)
{
profilePathTextBox.Text = profilePath.SelectedPath;
}
else
{
profilePathTextBox.Text = "Please Specify The Profile Path";
}
}
【讨论】:
如果您不想要一个糟糕的、非用户友好的对话*,请尝试Ookii.Dialogs 或查看How do you configure an OpenFileDialog to select folders? 的其他答案。我看到 Ookii 的唯一缺点是它需要 .NET 4 Full,而不仅仅是客户端配置文件。但是源代码包含在下载中,所以我将继续努力。可惜许可证不是 LGPL 或类似的......
另见:WinForms message box with textual buttons
*这是 FolderBrowserDialog 的样子:
【讨论】: