社区
C#
帖子详情
如何实现一个选择文件路径的对话框?
davidwee
2002-11-13 03:57:44
OpenFileDialog只能选择文件,如何让一个对话框只选择目录呢?
难道需要自己写一个么,如果那样的话,岂不是很麻烦?
...全文
1467
5
打赏
收藏
如何实现一个选择文件路径的对话框?
OpenFileDialog只能选择文件,如何让一个对话框只选择目录呢? 难道需要自己写一个么,如果那样的话,岂不是很麻烦?
复制链接
扫一扫
分享
转发到动态
举报
写回复
配置赞助广告
用AI写文章
5 条
回复
切换为时间正序
请发表友善的回复…
发表回复
打赏红包
烈马狂生
2002-11-13
打赏
举报
回复
我上边提供的有误。
下面提供另一个:
///<summary>
///功能名称:表示层选择路径
///功能描述:用户在备份过程中,当前空间已满后,选择新路径
///作 者:wxf 版本:1.0 ---2002-06-28---
///修 改:
///</summary>
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
namespace TJJ
{
/// <summary>
/// 名称:NewPath
/// 描述:用户在备份过程中,当前空间已满后,选择新路径
/// 作者:wxf 版本:1.0 ---2002-06-28---
/// 修改:
/// </summary>
public class NewPath : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TreeView treeView1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
public string newFilePath;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public NewPath()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.treeView1 = new System.Windows.Forms.TreeView();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// label1
//
this.label1.Location = new System.Drawing.Point(16, 12);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(80, 16);
this.label1.TabIndex = 0;
this.label1.Text = "请选择路径:";
//
// treeView1
//
this.treeView1.ImageIndex = -1;
this.treeView1.Location = new System.Drawing.Point(36, 36);
this.treeView1.Name = "treeView1";
this.treeView1.SelectedImageIndex = -1;
this.treeView1.Size = new System.Drawing.Size(356, 304);
this.treeView1.TabIndex = 1;
this.treeView1.AfterExpand += new System.Windows.Forms.TreeViewEventHandler(this.treeView1_AfterExpand);
this.treeView1.AfterSelect += new System.Windows.Forms.TreeViewEventHandler(this.treeView1_AfterSelect);
//
// button1
//
this.button1.Enabled = false;
this.button1.Location = new System.Drawing.Point(228, 348);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(68, 24);
this.button1.TabIndex = 2;
this.button1.Text = "确 定";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(296, 348);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(68, 24);
this.button2.TabIndex = 3;
this.button2.Text = "取 消";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// NewPath
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(424, 385);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.button2,
this.button1,
this.treeView1,
this.label1});
this.Name = "NewPath";
this.Text = "NewPath";
this.Load += new System.EventHandler(this.NewPath_Load);
this.ResumeLayout(false);
}
#endregion
private void NewPath_Load(object sender, System.EventArgs e)
{
if(!GetLocalDrivers())
{
MessageBox.Show("显示本地驱动器出错!");
return;
}
}
/// <summary>
/// 取得本地驱动器,初始化ListView
/// </summary>
/// <returns>初始化成功返回true</returns>
private bool GetLocalDrivers()
{
try
{
string[] Drivers=System.IO.Directory.GetLogicalDrives();
treeView1.Nodes.Clear();
System.Windows.Forms.TreeNode rootNode;
for(int i=0;i<Drivers.Length;i++)
{
rootNode=new System.Windows.Forms.TreeNode(Drivers[i]);
treeView1.Nodes.Add(rootNode);
if(!GetSubDirectory(rootNode.FullPath,rootNode))
{
return false;
}
}
return true;
}
catch
{
return false;
}
}
/// <summary>
/// 取得当前文件夹的子文件夹,并添加为它的子节点
/// </summary>
/// <param name="FullPath">当前节点的路径</param>
/// <param name="CurrentNode">当前节点</param>
/// <returns>操作成功返回true</returns>
private bool GetSubDirectory(string FullPath,System.Windows.Forms.TreeNode CurrentNode)
{
try
{
CurrentNode.Nodes.Clear();
string myTemp;
string[] Directorys=System.IO.Directory.GetDirectories(FullPath);
System.Windows.Forms.TreeNode subNode;
for(int i=0;i<Directorys.Length;i++)
{
myTemp=Directorys[i].Remove(0,FullPath.Length);
subNode=new System.Windows.Forms.TreeNode(myTemp);
CurrentNode.Nodes.Add(subNode);
}
return true;
}
catch
{
return false;
}
}
/// <summary>
/// 选中某个节点时,取得当前指定的路径
/// </summary>
/// <param name="sender">事件触发者</param>
/// <param name="e">时间参数</param>
private void treeView1_AfterSelect(object sender, System.Windows.Forms.TreeViewEventArgs e)
{
newFilePath=treeView1.SelectedNode.FullPath;
button1.Enabled=true;
System.Windows.Forms.TreeNodeCollection myNodeCollection=treeView1.SelectedNode.Nodes;
foreach( System.Windows.Forms.TreeNode myTreeNode in myNodeCollection)
{
if(!GetSubDirectory(myTreeNode.FullPath,myTreeNode))
{
MessageBox.Show("当前节点展开时,添加子节点出错!");
return;
}
}
treeView1.Update();
}
/// <summary>
/// 某个节点展开时,给它的子节点增加子节点
/// </summary>
/// <param name="sender">事件触发者</param>
/// <param name="e">时间参数</param>
private void treeView1_AfterExpand(object sender, System.Windows.Forms.TreeViewEventArgs e)
{
System.Windows.Forms.TreeNodeCollection myNodeCollection=e.Node.Nodes;
foreach( System.Windows.Forms.TreeNode myTreeNode in myNodeCollection)
{
if(!GetSubDirectory(myTreeNode.FullPath,myTreeNode))
{
MessageBox.Show("当前节点展开时,添加子节点出错!");
return;
}
}
treeView1.Update();
}
/// <summary>
/// 取消
/// </summary>
/// <param name="sender">事件触发者</param>
/// <param name="e">时间参数</param>
private void button2_Click(object sender, System.EventArgs e)
{
newFilePath="";
this.Dispose();
}
/// <summary>
/// 确定
/// </summary>
/// <param name="sender">事件触发者</param>
/// <param name="e">时间参数</param>
private void button1_Click(object sender, System.EventArgs e)
{
this.Dispose();
}
}
}
yarshray
2002-11-13
打赏
举报
回复
http://www.codeproject.com/cs/miscctrl/folderseldlg.asp
烈马狂生
2002-11-13
打赏
举报
回复
为什么不用SaveFileDialog 多么简单呀。
我就是这么用的。效果很好。
idiotzeng
2002-11-13
打赏
举报
回复
http://expert.csdn.net/Expert/topic/1155/1155973.xml?temp=.0625574
Dugu_Niu
2002-11-13
打赏
举报
回复
从旧贴中找找,多着呢
windows API
实现
用户
选择
文件
路径
的
对话框
本文介绍如何使用Windows API函数SHBrowseForFolder和SHGetPathFromIDList
实现
文件
路径
的
选择
功能。通过示例代码展示了如何创建
一个
文件
选择
对话框
,并从用户
选择
中获取
文件
路径
。
python
实现
对话框
文件
选择
以及
文件
路径
选择
本文介绍了如何在Python中使用tkinter库和filedialog模块创建
文件
选择
和
路径
选择
对话框
。通过简单的四步操作,包括导入库、创建Tkinter窗口、调用filedialog方法获取
文件
或
路径
,
实现
了用户友好的
文件
选择
功能。示例代码展示了
选择
文件
后的
路径
打印,适用于需要用户交互选取
文件
的场景。
qt
实现
对话框
选择
文件
路径
并保存(简易版)
本文介绍如何在Qt应用程序中使用QFileDialog组件来
实现
文件
选择
对话框
的功能。通过实例展示了如何创建QWidget并调用QFileDialog::getOpenFileName方法来获取用户
选择
的
文件
路径
,并使用QMessageBox显示该
路径
。
qt
实现
对话框
选择
文件
路径
并保存
展示了在C++Qt程序中通过QFileDialog获取用户
文件
打开和保存
路径
的方法。,
选择
文件
路径
对话框
被覆盖的问题
本文介绍了解决
对话框
置顶状态时,
选择
文件
路径
按钮导致弹出
对话框
被隐藏的问题。通过将BROWSEINFO的hwndOwner设置为m_hWnd,
实现
了在置顶
对话框
中正常显示
选择
文件
路径
对话框
的功能。
C#
111,129
社区成员
642,540
社区内容
发帖
与我相关
我的任务
C#
.NET技术 C#
复制链接
扫一扫
分享
社区描述
.NET技术 C#
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
让您成为最强悍的C#开发者
试试用AI创作助手写篇文章吧
+ 用AI写文章