using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.IO;
namespace txttest2
{
/// <summary>
/// PathDlgSelect 的摘要说明。
/// </summary>
public class PathDlgSelect : System.Windows.Forms.Form
{
private static string driveLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
// 用于列举驱动器盘符
private DirectoryInfo folder;
private DevExpress.XtraEditors.SimpleButton simpleButton1;
private DevExpress.XtraEditors.SimpleButton simpleButton2;
private DevExpress.XtraEditors.TextEdit textEdit1;
private System.Windows.Forms.TreeView treeView1;
private DevExpress.XtraTreeList.TreeList treeList1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.ImageList imageList1;
private System.Windows.Forms.TextBox textBox1;
private System.ComponentModel.IContainer components;
public PathDlgSelect()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
InitPathDlg();// 该函数调用完成目录选择对话框的初始设置
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(PathDlgSelect));
this.simpleButton1 = new DevExpress.XtraEditors.SimpleButton();
this.simpleButton2 = new DevExpress.XtraEditors.SimpleButton();
this.textEdit1 = new DevExpress.XtraEditors.TextEdit();
this.treeView1 = new System.Windows.Forms.TreeView();
this.imageList1 = new System.Windows.Forms.ImageList(this.components);
this.treeList1 = new DevExpress.XtraTreeList.TreeList();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.textBox1 = new System.Windows.Forms.TextBox();
((System.ComponentModel.ISupportInitialize)(this.textEdit1.Properties)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.treeList1)).BeginInit();
this.SuspendLayout();
//
// simpleButton1
//
this.simpleButton1.Location = new System.Drawing.Point(584, 368);
this.simpleButton1.Name = "simpleButton1";
this.simpleButton1.TabIndex = 0;
this.simpleButton1.Text = "选择";
this.simpleButton1.Click += new System.EventHandler(this.simpleButton1_Click);
//
// simpleButton2
//
this.simpleButton2.Location = new System.Drawing.Point(664, 368);
this.simpleButton2.Name = "simpleButton2";
this.simpleButton2.TabIndex = 1;
this.simpleButton2.Text = "取消";
this.simpleButton2.Click += new System.EventHandler(this.simpleButton2_Click);
//
// textEdit1
//
this.textEdit1.EditValue = "textEdit1";
this.textEdit1.Location = new System.Drawing.Point(96, 6);
this.textEdit1.Name = "textEdit1";
this.textEdit1.Size = new System.Drawing.Size(336, 23);
this.textEdit1.TabIndex = 2;
//
// treeView1
//
this.treeView1.ImageList = this.imageList1;
this.treeView1.Location = new System.Drawing.Point(8, 64);
this.treeView1.Name = "treeView1";
this.treeView1.SelectedImageIndex = 1;
this.treeView1.Size = new System.Drawing.Size(232, 288);
this.treeView1.TabIndex = 3;
this.treeView1.BeforeSelect += new System.Windows.Forms.TreeViewCancelEventHandler(this.treeView1_BeforeSelect);
this.treeView1.BeforeExpand += new System.Windows.Forms.TreeViewCancelEventHandler(this.treeView1_BeforeExpand);
//
// imageList1
//
this.imageList1.ImageSize = new System.Drawing.Size(16, 16);
this.imageList1.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imageList1.ImageStream")));
this.imageList1.TransparentColor = System.Drawing.Color.Transparent;
//
// treeList1
//
this.treeList1.Location = new System.Drawing.Point(256, 64);
this.treeList1.Name = "treeList1";
this.treeList1.Size = new System.Drawing.Size(528, 288);
this.treeList1.TabIndex = 4;
//
// label1
//
this.label1.Location = new System.Drawing.Point(16, 12);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(72, 12);
this.label1.TabIndex = 5;
this.label1.Text = "完整路径:";
//
// label2
//
this.label2.Location = new System.Drawing.Point(16, 40);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(152, 16);
this.label2.TabIndex = 6;
this.label2.Text = "请选择一个文件夹:";
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(440, 8);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(304, 21);
this.textBox1.TabIndex = 7;
this.textBox1.Text = "textBox1";
//
// PathDlgSelect
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(800, 421);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.treeList1);
this.Controls.Add(this.treeView1);
this.Controls.Add(this.textEdit1);
this.Controls.Add(this.simpleButton2);
this.Controls.Add(this.simpleButton1);
this.Name = "PathDlgSelect";
this.Text = "PathDlgSelect";
((System.ComponentModel.ISupportInitialize)(this.textEdit1.Properties)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.treeList1)).EndInit();
this.ResumeLayout(false);
}
#endregion
private void simpleButton1_Click(object sender, System.EventArgs e)
{
this.DialogResult = DialogResult.OK;
this.Close();
}
private void InitPathDlg()
{
DirectoryInfo directory;
string sCurPath = ""; // 重新清空
treeView1.Nodes.Clear();
// 将硬盘上的所有的驱动器都列举出来
foreach( char c in driveLetters )
{
sCurPath = c + "://";
try
{
// 获得该路径的目录信息
directory = new DirectoryInfo(sCurPath);
// 如果获得的目录信息正确,则将它添加到目录树视中
if ( directory.Exists == true )
{
TreeNode newNode = new TreeNode(directory.FullName);
treeView1.Nodes.Add(newNode); // 添加新的节点到根节点
getSubDirs(newNode);
// 调用getSubDirs()函数,检查该驱动器上的任何存在子目录
}
}
catch( Exception doh)
{
Console.WriteLine(doh.Message);
}
}
}
private void getSubDirs( TreeNode parent )
{
DirectoryInfo directory;
try
{
// 如果还没有检查过这个文件夹,则检查之
if ( parent.Nodes.Count == 0 )
{
directory = new DirectoryInfo(parent.FullPath);
foreach( DirectoryInfo dir in directory.GetDirectories())
{
// 新建一个数节点,并添加到目录树视
TreeNode newNode = new TreeNode(dir.Name);
parent.Nodes.Add(newNode);
}
}
foreach(TreeNode node in parent.Nodes)
{
// 如果还没有检查过这个文件夹,则检查
if (node.Nodes.Count == 0)
{
directory = new DirectoryInfo(node.FullPath);
// 检查该目录上的任何子目录
foreach( DirectoryInfo dir in directory.GetDirectories())
{
// 新建一个数节点,并添加到目录树视
TreeNode newNode = new TreeNode(dir.Name);
node.Nodes.Add(newNode);
}
}
}
}
catch( Exception doh )
{
Console.WriteLine(doh.Message);
}
}
private string fixPath( TreeNode node )
{
string sRet = "";
try
{
sRet = node.FullPath;
int index = sRet.IndexOf("////");
if (index > 1 )
{
sRet = node.FullPath.Remove(index, 1);
}
}
catch( Exception doh )
{
Console.WriteLine(doh.Message);
}
return sRet;
}
private void treeView1_BeforeSelect(object sender, System.Windows.Forms.TreeViewCancelEventArgs e)
{
getSubDirs(e.Node); // 取得选择节点的子文件夹
textBox1.Text = fixPath(e.Node); // 更新文本框内容
folder = new DirectoryInfo(e.Node.FullPath); // 获得它的目录信息
}
private void treeView1_BeforeExpand(object sender, System.Windows.Forms.TreeViewCancelEventArgs e)
{
getSubDirs(e.Node); // 取得选择节点的子文件夹
textBox1.Text = fixPath(e.Node); // 更新文本框内容
folder = new DirectoryInfo(e.Node.FullPath); // 获得它的目录信息
}
private void simpleButton2_Click(object sender, System.EventArgs e)
{
folder = null;
this.Close();
}
public string name// 返回所选择的目录的名称
{
get { return ((folder != null && folder.Exists))? folder.Name : null; }
}
public string fullPath// 返回所选择的目录的完整路径
{
get { return ((folder != null && folder.Exists && treeView1.SelectedNode != null))? fixPath(treeView1.SelectedNode) : null; }
}
public DirectoryInfo info// 返回所选择的目录的信息
{
get { return ((folder != null && folder.Exists))? folder : null; }
}
}
}
PathDlgSelect 是一个自定义的文件夹选择窗体,它允许用户在多个驱动器和子目录中选择一个文件夹。窗体包含一个树形视图组件(TreeView)展示驱动器和子目录,以及一个文本框显示所选路径。用户可以点击'选择'按钮确认选择,或点击'取消'按钮关闭窗口。窗体还处理异常,确保在枚举驱动器和目录时的稳定性。
4685

被折叠的 条评论
为什么被折叠?



