C# winform 组件---- folderBrowserDialog与openFileDialog(转)

时间:2024-05-01 15:19:03
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)的用法

C# winform 组件---- folderBrowserDialog与openFileDialog(转) C# winform 组件---- folderBrowserDialog与openFileDialog(转) 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.主畫面

C# winform 组件---- folderBrowserDialog与openFileDialog(转)

2.選目錄

C# winform 组件---- folderBrowserDialog与openFileDialog(转)

3.選檔案

C# winform 组件---- folderBrowserDialog与openFileDialog(转)

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