這篇文章主要介紹了C#使用瀏覽按鈕獲得檔案路徑和檔案夾路徑的方法,結合執行個體形式分析了C#瀏覽器事件響應及檔案操作相關技巧,需要的朋友可以參考下
本文執行個體講述了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(); } }}
瀏覽按鈕下的事件
private void btnChoose_Click(object sender, EventArgs e){ FolderDialog fDialog = new FolderDialog(); fDialog.DisplayDialog(); this.tbfilePath.Text = fDialog.Path;}