C#利用瀏覽按鈕獲得文件路徑和文件夾路徑


生成文件夾路徑

private void btnChoose_Click(object sender, EventArgs e)

        {
            using (OpenFileDialog dialog = new OpenFileDialog())
            {
                dialog.Multiselect = true;
                if (dialog.ShowDialog() == DialogResult.OK)
                {
                    try
                    {
                        this.tbFilePath.Text = dialog.FileName;
                    }
                    catch(Exception ex)
                    {
                        throw(ex);
                    }
                }
            }

生成文件路徑

新建一個FolderDialog類(重載FolderNameEditor)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms.Design;
using System.Windows.Forms;

namespace   Common
{
    class FolderDialog:FolderNameEditor
    {
        FolderBrowser fDialog = new FolderBrowser();
        public FolderDialog(){ }

        public DialogResult DisplayDialog()
        {
            return DisplayDialog("請選擇一個文件夾");
        }

        public DialogResult DisplayDialog(string description)
        {
            fDialog.Description = description;
            return fDialog.ShowDialog();
        }

        public string Path
        {
            get
            {
                return fDialog.DirectoryPath;
            }
        }

        ~FolderDialog()
        {
            fDialog.Dispose();
        }
    }
}

http://hovertree.com/menu/winform/

瀏覽按鈕下的事件

private void btnChoose_Click(object sender, EventArgs e)
        {
            FolderDialog fDialog = new FolderDialog();
            fDialog.DisplayDialog();
            this.tbfilePath.Text = fDialog.Path;
        }

推薦:http://www.cnblogs.com/roucheng/p/DGVHeaderText.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM