2009-06-27 13:36 2153人阅读 评论(1) 收藏 举报

vs2008 winform  的工具箱中有两个组件:folderBrowserDialog与openFileDialog.他们的作用如下:

folderBrowserDialog:打开一个浏览对话框,以便选取路经.
openFileDialog: 打开一个浏览对话框,以便选取一个文件名.

在实际操作中的应用如下:

private void button1_Click(object sender, EventArgs e)
        {
            folderBrowserDialog1.ShowDialog();
            this.textBox1.Text = folderBrowserDialog1.SelectedPath;
        }

private void button2_Click(object sender, EventArgs e)
        {
            openFileDialog1.ShowDialog();
            this.textBox2.Text = openFileDialog1.SafeFileName;
        }

如果没有在窗体中拖入folderBrowserDialog与openFileDialog组件,代码也可以这样来写:
using System.Collections.Generic;

    1. using System.ComponentModel;
    2. using System.Data;
    3. using System.Drawing;
    4. using System.Text;
    5. using System.Windows.Forms;
    6. namespace WindowsApplication1
    7. {
    8. public partial class SelectFolder : Form
    9. {
    10. public SelectFolder()
    11. {
    12. InitializeComponent();
    13. }
    14. private void btnSelectPath_Click(object sender, EventArgs e)
    15. {
    16. FolderBrowserDialog path = new FolderBrowserDialog();
    17. path.ShowDialog();
    18. this.txtPath.Text = path.SelectedPath;
    19. }
    20. private void btnSelectFile_Click(object sender, EventArgs e)
    21. {
    22. OpenFileDialog file = new OpenFileDialog();
    23. file.ShowDialog();
    24. this.txtFile.Text = file.SafeFileName;
    25. }
    26. }

    27. [C#]Winform選擇目錄路徑(FolderBrowserDialog)與選擇檔案名稱(OpenFileDialog)的用法

  

最近寫winform的程式,剛好要用到這樣的功能

介紹如何利用FolderBrowserDialog與OpenFileDialog

來選擇目錄或檔案...

c#(winform)部分程式碼
SelectFolder.cs

01 <span style="display: none" id="1226045165047S"> </span>using System;
02 using System.Collections.Generic;
03 using System.ComponentModel;
04 using System.Data;
05 using System.Drawing;
06 using System.Text;
07 using System.Windows.Forms;
08  
09 namespace WindowsApplication1
10 {
11     public partial class SelectFolder : Form
12     {
13         public SelectFolder()
14         {
15             InitializeComponent();
16         }
17  
18         private void btnSelectPath_Click(object sender, EventArgs e)
19         {
20             FolderBrowserDialog path = new FolderBrowserDialog();
21             path.ShowDialog();
22             this.txtPath.Text = path.SelectedPath;
23         }
24  
25         private void btnSelectFile_Click(object sender, EventArgs e)
26         {
27             OpenFileDialog file = new OpenFileDialog();
28             file.ShowDialog();
29             this.txtFile.Text = file.SafeFileName;
30         }
31  
32     }
33 }

執行結果:

1.主畫面

2.選目錄

3.選檔案

參考網址:
http://www.codeproject.com/KB/cs/csFolderBrowseDialogEx.aspx

C# winform 组件---- folderBrowserDialog与openFileDialog(转)的更多相关文章

  1. 在WPF使用FolderBrowserDialog和OpenFileDialog

    原文 在WPF使用FolderBrowserDialog和OpenFileDialog 相信习惯以前winform开发的朋友们都对FolderBrowserDialog和OpenFileDialog这 ...

  2. vs2017 winform 组件 -- 总结

    1.ComboBox  [下拉框] (1) 添加选项 this.[控件名].Items.Add("内容") (2)设置下拉框 自动完成 模式 和 数据源 this.[控件名].Au ...

  3. WinForm中的ListBox组件编程

    ListBox组件是一个程序设计中经常使用到的组件,在Visual C#和Visual Basic .Net程序中使用这个组件,必须要在程序中导入.Net FrameWork SDK中名称空间Syst ...

  4. .net WinForm 的数据绑定

    .net WinForm 的数据绑定相当灵活 http://www.cnblogs.com/ydong/archive/2006/04/22/381847.html 原来只知道 Control 类上的 ...

  5. FtpWebRequest与FtpWebResponse完成FTP操作

    WebRequestMethods.Ftp类: 表示可与 FTP 请求一起使用的 FTP 协议方法的类型. Append​File    表示要用于将文件追加到 FTP 服务器上的现有文件的 FTP ...

  6. c#第三方控件地址

    原文:http://blog.csdn.net/wpcxyking/article/details/6249825 首先感谢博文原者,分享这么有价值的内容,特此感谢. DevExpress 出品 Dx ...

  7. C# 選擇本機檔案並上傳

    參考自:http://www.dotblogs.com.tw/puma/archive/2008/11/07/5910.aspxhttp://www.codeproject.com/Articles/ ...

  8. VB.NET数据库编程基础教程

    关键词:作者罗姗   众所周知,VB.NET自身并不具备对数据库进行操作的功能,它对数据库的处理是通过.NET FrameWork SDK中面向数据库编程的类库和微软的MDAC来实现的.其中,ADO. ...

  9. C#使用NanUI或ChromiumFx碰到的坑(一)

    最近在花时间封装一个Razor模板+NanUI的Winform组件,发现了有个神奇地方,,由于需要使用CfxResourceHandler,用于把对cshtml文件的请求,编译成html并返回给CEF ...

随机推荐

  1. 【sqli-labs】 less54 GET -Challenge -Union -10 queries allowed -Variation1 (GET型 挑战 联合查询 只允许10次查询 变化1)

    尝试的次数只有10次 http://192.168.136.128/sqli-labs-master/Less-54/index.php?id=1' 单引号报错,错误信息没有显示 加注释符页面恢复正常 ...

  2. ( 转)Hibernate常用API

    http://blog.csdn.net/yerenyuan_pku/article/details/65103203 可在度娘上摘抄如下文字: Hibernate的核心类和接口一共有6个,分别为:S ...

  3. Memcached 在Linux上的安装

    1.安装libevent wget https://github.com/libevent/libevent/releases/download/release-2.1.8-stable/libeve ...

  4. CAD在网页中得到批注信息

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 3 ...

  5. 洛谷——P1063 能量项链

    P1063 能量项链 题目描述 在MarsMars星球上,每个MarsMars人都随身佩带着一串能量项链.在项链上有NN颗能量珠.能量珠是一颗有头标记与尾标记的珠子,这些标记对应着某个正整数.并且,对 ...

  6. Luogu P2298 Mzc和男家丁的游戏

    Mzc和男家丁的游戏 题目背景 mzc与djn的第二弹. 题目描述 mzc家很有钱(开玩笑),他家有n个男家丁(做过上一弹的都知道).他把她们召集在了一起,他们决定玩捉迷藏.现在mzc要来寻找他的男家 ...

  7. MySQL多表连接操作

    select * from userinfo ,dapartment where userinfo.part_id = dapartment.id; --左连接: 左边全部显示 select * fr ...

  8. 面向对象:classmethod、staticmethod、property

    一.classmethod(类方法).staticmethod(静态方法) 方法包括:普通方法.类方法和静态方法,三种方法在内存中都归属于类,区别在于调用方式不同. # 普通方法 由对象调用,至少一个 ...

  9. 面试题:你能写一个Vue的双向数据绑定吗?

    在目前的前端面试中,vue的双向数据绑定已经成为了一个非常容易考到的点,即使不能当场写出来,至少也要能说出原理.本篇文章中我将会仿照vue写一个双向数据绑定的实例,名字就叫myVue吧.结合注释,希望 ...

  10. BUAA_OO_博客作业二

    1.作业设计策略 1.1第一次作业 ​ 第一次作业指导书要求是一个单部多线程傻瓜调度(FAFS)电梯的模拟,由于为了可扩展性和模块化设计,第一次作业我采用了三线程,即输入处理线程,调度器线程,电梯线程 ...