第493篇--OpenFolderDialog implementation
If you have developed a Windows Form project, you must haved experienced the OpenFileDialog class, but WinForm does not provided a class named OpenFolderDialog. Thus today, I'd like to share with your the implementation of OpenFolderDialog class as below.
First, you need to add the reference to System.Design.Dll.
ScreenSnap:
Code snippet:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Windows.Forms.Design; namespace WindowsFormsOpenFile { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { using (OpenFolderDialog openFolderDlg = new OpenFolderDialog()) { if (openFolderDlg.ShowDialog() == DialogResult.OK) { textBox1.Text = openFolderDlg.Path; } } } } public class OpenFolderDialog : FolderNameEditor, IDisposable { FolderNameEditor.FolderBrowser fDialog = new FolderNameEditor.FolderBrowser(); public OpenFolderDialog() { } public DialogResult ShowDialog() { return ShowDialog("Select a folder:"); } public DialogResult ShowDialog(string description) { fDialog.Description = description; return fDialog.ShowDialog(); } public string Path { get { return fDialog.DirectoryPath; } } public void Dispose() { fDialog.Dispose(); } } }
If you still have any questions, please contact me via Sina micro-blogging: @上海jimzhou
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.Design;
namespace WindowsFormsOpenFile
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
using (OpenFolderDialog openFolderDlg = new OpenFolderDialog())
{
if (openFolderDlg.ShowDialog() == DialogResult.OK)
{
textBox1.Text = openFolderDlg.Path;
}
}
}
}
public class OpenFolderDialog : FolderNameEditor, IDisposable
{
FolderNameEditor.FolderBrowser fDialog = new FolderNameEditor.FolderBrowser();
public OpenFolderDialog()
{
}
public DialogResult ShowDialog()
{
return ShowDialog("Select a folder:");
}
public DialogResult ShowDialog(string description)
{
fDialog.Description = description;
return fDialog.ShowDialog();
}
public string Path
{
get
{
return fDialog.DirectoryPath;
}
}
public void Dispose()
{
fDialog.Dispose();
}
}
}
Save file dialogue:
private void SaveButton_Click(object sender, EventArgs e) { SaveFileDialog saveFileDialog1 = new SaveFileDialog(); saveFileDialog1.InitialDirectory = @"C:\"; saveFileDialog1.Title = "Save text Files"; saveFileDialog1.CheckFileExists = true; saveFileDialog1.CheckPathExists = true; saveFileDialog1.DefaultExt = "txt"; saveFileDialog1.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*"; saveFileDialog1.FilterIndex = 2; saveFileDialog1.RestoreDirectory = true; if (saveFileDialog1.ShowDialog() == DialogResult.OK) { //textBox1.Text = saveFileDialog1.FileName; } }
Another version:
private void SaveFileDialog()
{
//string localFilePath, fileNameExt, newFileName, FilePath;
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
//设置文件类型
saveFileDialog1.Filter = " txt files(*.txt)|*.txt|All files(*.*)|*.*";
//设置默认文件类型显示顺序
saveFileDialog1.FilterIndex = 2;
//保存对话框是否记忆上次打开的目录
saveFileDialog1.RestoreDirectory = true;
//点了保存按钮进入
if (saveFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
var res = saveFileDialog1.FileName;
//获得文件路径
//localFilePath = saveFileDialog1.FileName.ToString();
//获取文件名,不带路径
//fileNameExt = localFilePath.Substring(localFilePath.LastIndexOf("//") + 1);
//获取文件路径,不带文件名
//FilePath = localFilePath.Substring(0, localFilePath.LastIndexOf("//"));
//给文件名前加上时间
//newFileName = DateTime.Now.ToString("yyyyMMdd") + fileNameExt;
//在文件名里加字符
//saveFileDialog1.FileName.Insert(1,"dameng");
//System.IO.FileStream fs = (System.IO.FileStream)saveFileDialog1.OpenFile();//输出文件
//fs输出带文字或图片的文件,就看需求了
}
}
/// <summary>
/// Open File Dialog
/// </summary>
private void OpenFileDialog()
{
string path = string.Empty;
using (OpenFileDialog ofd = new OpenFileDialog())
{
//定义打开的默认文件夹位置
ofd.Filter = " txt files(*.txt)|*.txt|All files(*.*)|*.*";
ofd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
path = ofd.FileName;
}
}
}