下載
//儲存檔案夾選擇對話方塊引用
//添加引用system.design.dll /
using System.Windows.Forms.Design;
private void button4_Click(object sender, System.EventArgs e)
{
//選擇檔案的儲存路徑
//添加表單控制項folderBrowserDialog1
DirBrowser olderBrowserDlg=new DirBrowser();
if (folderBrowserDialog1.ShowDialog()==DialogResult.OK)
{
textBox2.Text = folderBrowserDialog1.SelectedPath;
}
}
#region 彈出儲存檔案夾選擇視窗類別
//一般選擇檔案儲存地址都用彈出對話方塊來進行選擇
public class DirectorySelect : FolderNameEditor
{
private FolderBrowser fb = new FolderBrowser();
private string fDescription = "Choose Directory";
private string fReturnPath = String.Empty;
public string Description
{
set { fDescription = value; }
get { return fDescription; }
}
public string ReturnPath
{
get { return fReturnPath; }
}
public DirectorySelect()
{
}
private DialogResult RunDialog()
{
fb.Description = this.Description;
fb.StartLocation = FolderBrowserFolder.MyComputer;
fb.Style = FolderBrowserStyles.RestrictToSubfolders;
//|FolderBrowserStyles.RestrictToDomain;
return fb.ShowDialog();
}
public DialogResult ShowDialog()
{
DialogResult dRes = DialogResult.None;
dRes = RunDialog();
if (dRes == DialogResult.OK)
this.fReturnPath = fb.DirectoryPath;
else
this.fReturnPath = String.Empty;
return dRes;
}
}
//一般選擇檔案儲存地址都用彈出對話方塊來進行選擇
//調用
//DirBrowser myDirBrowser=new DirBrowser();
//if(myDirBrowser.ShowDialog()!=DialogResult.Cancel)
//MessageBox.Show(myDirBrowser.ReturnPath);
public class DirBrowser : FolderNameEditor
{
FolderBrowser fb = new FolderBrowser();
public string Description
{
set { _description = value; }
get { return _description; }
}
public string ReturnPath
{
get { return _returnPath; }
}
public DirBrowser() { }
public DialogResult ShowDialog()
{
fb.Description = _description;
fb.StartLocation = FolderBrowserFolder.MyComputer;
DialogResult r = fb.ShowDialog();
if (r == DialogResult.OK)
_returnPath = fb.DirectoryPath;
else
_returnPath = String.Empty;
return r;
}
//private string _description = "Choose Directory";
//private string _returnPath = String.Empty;
private string _description = "請選擇檔案夾";
private string _returnPath = String.Empty;
}
#endregion