选择文件夹路径的窗体

C#选择文件、选择文件夹、打开文件(或者文件夹) 1、选择文件用OpenDialog OpenFileDialog dialog = new OpenFileDialog(); dialog.Multiselect = true;//该值确定是否可以选择多个文件 dialog.Title = "请选择文件夹"; dialog.Filter = "所有文件(*.*)|*.*"; if (dialog.ShowDialog()... 阅读详情

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

C#winForm窗体,以窗口对话形式选择文件夹、文件路径 C#winForm程序开发,以对话窗口的形式选择文件夹路径。 阅读详情

相关推荐

VB6 选择文件夹路径

'--------------------------------------------------------------------------------------- ' Module : ModuleFile ' Author : ROVAST ' Date : 2014-4-22 ' Purpose : 文件相关操作模块 ' Function ...

weixin_30642561的博客 1227

关于如何将项目中繁多DevExpress的dll存放到指定文件夹

一个标准的Dev项目,一般情况下都会引用到众多的DevExpress的相关dll,如果全部在启动路径下的话,着实不雅。所以有必要把Dev的这些dll存放到指定路径,也方便管理,步骤如下 1、在项目中,将Dev的所有dll的复制到本地的属性修改成false; 2、为项目添加一个配置文件app.config,并复制以下内容,其中devdlls即为你要存放的dev的相关dll文件夹名 &...

涛神-Dev 的博客 3万+

C#学习笔记005——选择文件、文件夹操作

工具:VS2019 项目类型:C# Windows 窗体应用(.NET Framework) 框架:.NET Framework 4 控件:窗体、按钮(Button)、openFileDialog 功能简述:点击按钮,弹出选择文件的窗体 界面:仅用一个按钮测试功能 一、选择文件(利用OpenFileDialog控件) (一)编辑按钮动作 双击按钮,添加以下代码: private void button1_Click(object sender, EventArgs e

SYW的博客 2万+

一篇文章教你如何用界面组件DevExpress WPF为应用配置文件选择!

本文将为大家介绍如何使用DevExpress WPF的BrowsePathEdit组件在WPF应用程序中配置文件/文件夹选择,欢迎下载最新版组件体验~

需要界面开发、IDE工具研发中文教程资料的小伙伴,记得私信我~ 1121

C#实现选择文件夹对话框(附完整源码)

C#实现选择文件夹对话框(附完整源码)

希望我的博客,能帮上你解决学习中工作中所遇到的问题 220

C#实现文件夹选择框(附完整源码)

C#实现文件夹选择框(附完整源码)

希望我的博客,能帮上你解决学习中工作中所遇到的问题 553

使用 c# + vue 制作 DevExpress 报表

DevExpress 下载地址: DevExpress Reporting Installer | .NET Reporting Tools | DevExpress Documentation选择你要放置的文件夹,依次选择 “”, “”第一次显示时可能没有详情页面,点击右下角 “”选择 “” 模板 若工具栏没有显示,可以按下 “” 呼出还有一些其他工具栏可从上方 中选择点击右上方小正方形添加数据源将你自己的 json 数据粘贴到下方,它会自动解析为对象 添加完毕后拖动数据制作报表点击下方按钮即可预览下

xy的博客 1863

DevExpress安装程序

DevExpressUniversalTrialSetup-20210401为安装文件

DEV 控件使用

1.单元格显示时间格式(yyyy/MM/dd HH:mm) 2.pictureEdit 展示图片(点击按钮选中图片,并展示) OpenFileDialog openFileDialog = new OpenFileDialog(); openFileDialog.Title = "选择图片文件"; o...

qq_32002821的博客 2052

Calibre LVS错误排查实战:从Innovus到GDS的完整调试流程(附常见错误解析)

本文详细介绍了数字IC后端设计中,从Innovus工具输出数据到Calibre LVS物理验证的完整调试流程。文章系统性地讲解了LVS错误排查的方法论,包括报告解读、图形化定位以及针对器件不匹配、电源网络错误、短路开路等常见问题的深度解析与解决方案,旨在帮助工程师高效完成版图与原理图的一致性检查。

weixin_29218963的博客 779

Comsol实战:铌酸锂x切与z切参数设置全攻略(附电场强度归一化技巧)

本文详细介绍了在Comsol Multiphysics中设置铌酸锂x切与z切晶体参数的全流程,包括晶体取向定义、电光系数配置及关键光学参数计算。重点讲解了电场强度归一化这一实用技巧,帮助用户提升仿真分析效率,为设计电光调制器等集成光子器件提供精准的仿真基础。

year5的博客 584

c#选取文件夹路径_C#winForm窗体,以窗口对话形式选择文件夹、文件路径

C#winForm程序开发,以对话窗口的形式选择文件夹路径。/// /// 对话框形式选择文件夹路径/// /// 返回所选择的文件夹路径public string ChooseFolderPath(){FolderBrowserDialog fbd = new FolderBrowserDialog();fbd.RootFolder = System.Environment.SpecialFol...

weixin_42546392的博客 1202

玩转 Milvus(二):在 Ubuntu 22.04(WSL2)上安装 Milvus

本文详细介绍了在Ubuntu 22.04(WSL2)环境下安装Milvus向量数据库的两种单机模式:Milvus Lite和Milvus Standalone。Milvus Lite作为轻量级Python库,适合快速学习和小规模实验,只需通过pip安装即可使用。Milvus Standalone则支持Docker和Docker Compose两种部署方式,适合开发测试或小规模生产环境。文章对比了不同安装方式的优缺点,并提供了验证案例和常见问题解答,帮助开发者快速上手,实现简单的语义搜索功能。

企业级后端与 AI 应用开发双线探索 2507

WinForm打开一个文件夹(得到文件夹路径 2种方法)

方法一: Form窗体拖一个FolderBrowserDialog控件 在按钮事件中写入以下代码: //打开选择文件夹目录        private void btnOpen_Click(object sender, EventArgs e)        {             folderBrowserDialog1.RootFolder = Environment.Specia...

weixin_34185364的博客 746

奇异值分解(SVD)C++实现

奇异值分解SVD(紧奇异值分解,截断奇异值分解)C++实现

weixin_45804601的博客 4173

【ArcGIS微课1000例】0145:如何按照自定义形状裁剪数据框?

使用绘图 工具条上的新建圆工具 可创建一个椭圆,使其包含要在该数据框中显示的数据范围。. 在基础工具工具条上单击选择元素工具 ,然后选择圆形。. 在内容列表中右键单击要裁剪的数据框,然后单击属性。. 单击裁剪选项下拉菜单,然后选择裁剪至形状。. 单击数据框属性 对话框中的数据框选项卡。还可以更改裁剪形状轮廓的边框选项。打开软件,添加配套实验数据包中。修改椭圆的属性,使其透明。. 单击所选图形的轮廓。. 单击指定形状按钮。

智慧空间实验室 959

C# 提示浏览选择本地文件夹 .

在form窗体 上添加一个Button按钮一个TextBox文本框 添加两个名空间: using System.IO; using System.Diagnostics; 然后在BUTTON按钮下写事件 程序代码 private void button1_Click(object sender, EventArgs e) {      FolderBrowserDialog fol

一个程序员的人生 669

软考高级系统架构设计师历年论文真题深度分析(2011-2025)

本文系统分析了2011-2025年系统架构设计师论文真题,归纳出十大主题类别,包括架构风格、企业集成、数据技术、非功能属性等。研究发现新兴技术占比最高(18.8%),企业集成、数据架构和非功能属性为三大核心考点。论文命题呈现明显技术演进特征,从经典架构理论逐步转向云原生、AI融合等前沿方向。备考建议重点关注高频考点和技术前沿,同时强化场景化解决方案能力。分析表明,架构师考试持续跟踪行业技术发展,注重理论与实践结合,为考生提供了明确的备考方向。

一几文的编程学习笔记。 2066

C# 窗体应用(.FET Framework ) 打开文件操作

【代码】C# 窗体应用(.FET Framework ) 打开文件操作。

m0_70015578的博客 412

架构师之HBase vs MongoDB 技术选型与场景决策实战案例

面对海量数据和高并发查询,该选 HBase 还是 MongoDB?本文为你提供完整的决策框架!从数据模型对比、架构解析、六大真实场景(电信 CDR、金融行情、广告点击流、电商商品、社交档案、内容管理)到性能、一致性、运维全面对比,助你在 PB 级时序数据和灵活查询之间做出精准选择!

肖哥弹架构博客 1106

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

vb.net 常见属性

窗体属性 1)、ControlBox = true (true代表显示关闭按钮,false代表隐藏)   2)、MaxButton = false (false代表隐藏最大化,true代表显示)  3)、MinButton = false (flase代表隐藏最小化,true代表显示)   4)、BorderStyle = 1 - Fixed (代表不能通过拉动来改变窗体的大小)

huangle63的专栏 1109

霍尼韦尔SoftMaster使用说明

霍尼韦尔SoftMaster使用说明

C#利用浏览按钮获得文件路径和文件夹路径

生成文件夹路径 private void btnChoose_Click(object sender, EventArgs e)         {            using (OpenFileDialog dialog = new OpenFileDialog())            {                dialog.Multiselect = ...

weixin_34202952的博客 564

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

Winform如何从本地导入照片?并显示在窗体上

点击“添加图片”按钮: 打开本地文件夹,选中要导入的图片,点击打开按钮 实现以上功能: [代码 + 解释] //为按钮添加单击事件 private void but_Add_Click(object sender, EventArgs e) { //实例化文件夹 并打开它 OpenFileDialog file = new OpenFileDialog(); file.ShowDialog();

HuiDieDie的博客 2759

自学C# 7.C#中利用pictureBox控件直接显示

C#中利用pictureBox控件直接显示

orange_dpp的博客 2719
上一篇: 正在学设计模式呵,转载一篇如何保证一个窗体的实例运行
下一篇: AOP(Aspect-Oriented Programming)重要吗?
akeen
博客等级 码龄21年 0粉丝 5原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值