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;
using System.ComponentModel;
using System.Da
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication1
{
public partial class SelectFolder : Form
{
public SelectFolder()
{
InitializeComponent();
}
private void btnSelectPath_Click(object sender, EventArgs e)
{
FolderBrowserDialog path = new FolderBrowserDialog();
path.ShowDialog();
this.txtPath.Text = path.SelectedPath;
}
private void btnSelectFile_Click(object sender, EventArgs e)
{
OpenFileDialog file = new OpenFileDialog();
file.ShowDialog();
this.txtFile.Text = file.SafeFileName;
}
}
}
本文来自优快云博客,转载请标明出处:http://blog.youkuaiyun.com/cranejuan/archive/2009/06/27/4302914.aspx