最近在写一个上位机,需要选择文件和文件夹的功能,所以就查阅了一下,这里记录一下方便以后查阅。
文件夹选择框
private void button1_Click(object sender, EventArgs e)
{
FolderBrowserDialog folderDlg = new FolderBrowserDialog();
folderDlg.Description = "请选择文件夹";
if (folderDlg.ShowDialog() == DialogResult.OK)
{
textBox1.Text = System.IO.Path.GetFullPath(folderDlg.SelectedPath);//将选中的文件的路径传递给TextBox “FilePath”
}
}
文件选择框
private void button2_Click(object sender, EventArgs e)
{
OpenFileDialog fileDialog = new OpenFileDialog();
fileDialog.Title = "数据上传";//设置弹窗标题
fileDialog.InitialDirectory = @"C:\Users\EEG\Desktop\";//打开对话框后的初始目录
fileDialog.Filter = "数据文件|*.csv|所有文件|*.*";//设置读取文件类型
fileDialog.RestoreDirectory = false;//若为false,则打开对话框后为上次的目录。若为true,则为初始目录
fileDialog.Multiselect = true;//可以选择多个文件
if (fileDialog.ShowDialog() == DialogResult.OK)
{
textBox2.Text =Path.GetFullPath(fileDialog.FileName);//将选中的文件的路径传递给TextBox “FilePath”
}
}
工程文件
相关的工程文件我放在了CSDN资源里面。《C# 实现“文件夹选择框 & 文件选择框”》