选择文件夹路径的窗体

原创 2006年06月09日 14:18:00

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; }
  }
 }
}

Winform运行状态下的图标选择器(扩展DevExpress的Image Picker)

利用DevExpress 的(Image Picker)图标选择器扩展其功能,通用winform项目;由于自带的图标选择器无法在运行时调用,为此改写了自带的图标选择器,保持原有功能的同时更加完善操作(...

关于解决DevExpress用DevExpress patch工具破解后经常弹出试用框的问题

关于解决DevExpress用DevExpress patch工具破解后经常弹出试用框的问题

boundingRect()函数的使用方法

Calculates the up-right bounding rectangle of a point set. C++: Rect boundingRect(InputArray points...

拖动文件到窗体获取文件路径

  • 2010年11月27日 13:12
  • 41KB
  • 下载

(97)窗体练习:输出路径下的文件或者目录

需求:在文本框中输入一个目录,点击“转到”或者enter都能将目录对应的文件打印在文本区域中,若不是正确的路径,则弹出对话框...

VC创建不规则窗体(字体路径)

  • 2009年03月29日 14:40
  • 30KB
  • 下载

javaScript打开窗体函数的使用详解和路径转换问题 .

一 、Location对象的使用简介 location对象: location提供了关于当前打开窗口或者特定框架的url信息。一个多框架的窗口对象在location属性显示的是父窗口的URL,每个...
  • fzzsh
  • fzzsh
  • 2011年10月02日 11:16
  • 533

jsp_选择文件夹的路径

  • 2012年03月29日 09:02
  • 49KB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:选择文件夹路径的窗体
举报原因:
原因补充:

(最多只允许输入30个字)