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. iOS keychain入门

    学了很久的iOS,一直都是明文保存用户名和密码在本地,手机一般都是自己用的,而且非越狱手机东西也不怎么能拿到数据,所以也就没在乎那么多,当然,这是不科学的.悄悄的说,这块一直不是我写的~~~ 用户隐私 ...

  2. JS——祝愿墙

    注意事项: 1.for循环的下一层注册了事件的话,事件函数中关于变量i的节点元素是不允许出现的,因为在函数加载的时候,只会加载函数名,不会加载函数体,外层for循环会走完一边,变量i一直会停留在最后一 ...

  3. 一个有趣的 ”Validation of viewstate MAC failed” 错误的发现和解决

    在ASP.NET里面,View State使用较为广泛.它作为一个隐藏字段,可以帮助服务端”记住“客户端的改变,这样客户端 收到服务器对PostBack的响应后,仍然可以展现在PostBack之前设定 ...

  4. Caffe RPN :error C2220: warning treated as error - no 'object' file generated

    在 caffe里面添加rpn_layer.cpp之后,总是出现 error C2220: warning treated as error - no 'object' file generated 这 ...

  5. for mxl path

    废话不多说,直接上例子 简单明了 create table tb_class ( classId int , className ) ) go ,'一班') ,'二班') ,'三班') go crea ...

  6. PHP控制反转(IOC)和依赖注入(DI

    <?php class A { public $b; public $c; public function A() { //TODO } public function Method() { $ ...

  7. Ansible实现zabbix服务器agent端批量部署

    项目需求:由于搭建zabbix,需要每台服务器都需要安装监控端(agent)正常的的操作是一台一台去安装,这样确实有点浪费时间,这里为大家准备了一款开源 的自动化运维工具Ansible,相信大家也很熟 ...

  8. Atcoder Code Festival 2017 qual C 10.22 D题题解

    [题意概述] 给出一个只有小写字母的序列,问最少把序列分成几段可以满足每一段可以通过变换成为回文串.变换指的是交换子序列中的字母的位置. [题解] 我们把a~z分别设为2^0~2^25,每个子序列满足 ...

  9. [cf 599A]Patrick and Shopping

    傻逼题,但是我还是wa了一发. #include <iostream> using namespace std; int main() { long long a,b,c,Ans=0x7f ...

  10. [UOJ#35] [UOJ后缀数组模板题] 后缀排序 [后缀数组模板]

    后缀数组,解决字符串问题的有利工具,本题代码为倍增SA算法 具体解释详见2009年国家集训队论文 #include <iostream> #include <algorithm> ...