在文件路径字段中,我想要捕获目录路径,例如:
textbox1.Text = directory path
有人吗?
在文件路径字段中,我想要捕获目录路径,例如:
textbox1.Text = directory path
如果你想让用户选择文件夹,可以使用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属性设置为"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或查看其他答案如何配置OpenFileDialog以选择文件夹?。我唯一看到的缺点是它需要.NET 4完整版,而不仅仅是客户端配置文件。但源代码包含在下载中,所以我会处理它。可惜许可证不是LGPL或类似的...
另请参阅:带有文本按钮的WinForms消息框
*这就是FolderBrowserDialog的外观: