社区
C#
帖子详情
如何实现一个选择文件路径的对话框?
davidwee
2002-11-13 03:57:44
OpenFileDialog只能选择文件,如何让一个对话框只选择目录呢?
难道需要自己写一个么,如果那样的话,岂不是很麻烦?
...全文
1443
5
打赏
收藏
如何实现一个选择文件路径的对话框?
OpenFileDialog只能选择文件,如何让一个对话框只选择目录呢? 难道需要自己写一个么,如果那样的话,岂不是很麻烦?
复制链接
扫一扫
分享
转发到动态
举报
AI
作业
写回复
配置赞助广告
用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
打赏
举报
回复
从旧贴中找找,多着呢
VC++, MFC中
实现
选择
文件
夹,
选择
文件
对话框
"VC++, MFC 中
实现
选择
文件
夹和
选择
文件
对话框
" ...通过使用 BROWSEINFO 结构体和 SHBrowseForFolder 函数,我们可以
实现
选择
文件
夹
对话框
,而通过使用 CFileDialog 对象,我们可以
实现
文件
选择
功能。
mfc 用
对话框
打开并读取txt
文件
的小程序
首先,我们需要在Visual Studio中创建
一个
新的MFC应用程序项目,
选择
"
对话框
"作为应用程序类型。这会生成
一个
包含默认
对话框
资源的项目。 2. **添加编辑框控件** 在
对话框
编辑器中,我们找到"Edit Control"(IDC_...
Qt Creator 系列教程
- 创建
一个
多窗口程序,包括
一个
对话框
和
一个
主窗口。 - 当用户点击
对话框
中的按钮后,进入主窗口;直接关闭
对话框
则退出整个程序。 - 在主窗口中点击按钮会弹出另
一个
对话框
,无论该
对话框
如何关闭,都会返回主窗口...
实现
Java中的
文件
选择
对话框
例如,在Windows系统上,
文件
选择
对话框
可能是
一个
标准的Windows
文件
选择
对话框
,而...然后,我们可以创建
一个
简单的Java应用程序,并在其中使用JFileChooser类来
实现
文件
选择
对话框
。最后,我们打印出
选择
的
文件
路径
。
python通过
对话框
实现
文件
或
文件
夹
路径
的
选择
并获得
路径
在通过python保存一些
文件
的时候 ,想要通过
对话框
的方式来添加
文件
路径
网上可以查到很多资料,但是如果要通过
对话框
的方式来添加
文件
夹
路径
却查了好久也没有查到,最后经过一番查找加理解,终于
实现
了,我猜没有查...
C#
111,110
社区成员
642,556
社区内容
发帖
与我相关
我的任务
C#
.NET技术 C#
复制链接
扫一扫
分享
社区描述
.NET技术 C#
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
让您成为最强悍的C#开发者
试试用AI创作助手写篇文章吧
+ 用AI写文章