如何实现一个选择文件路径的对话框?

davidwee 2002-11-13 03:57:44
OpenFileDialog只能选择文件,如何让一个对话框只选择目录呢?
难道需要自己写一个么,如果那样的话,岂不是很麻烦?
...全文
1418 5 打赏 收藏 转发到动态 举报
写回复
用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
  • 打赏
  • 举报
回复
从旧贴中找找,多着呢

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧