本文打开文件、文件夹对话框操作是封装在类中进行操作,只需引用该类并调用相应的方法即可进行相关的操作。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace ProjectManagerTools
{
class DialogOperate
{
//打开文件对话框
public string OpenFile()
{
string strFileName = "";
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "Excel文件(*.xls;*.xlsx)|*.xls;*.xlsx|word文档(*.doc;*.docx)|*.doc;*.docx|所有文件|*.*";
ofd.ValidateNames = true; // 验证用户输入是否是一个有效的Windows文件名
ofd.CheckFileExists = true; //验证路径的有效性
ofd.CheckPathExists = true;//验证路径的有效性
if (ofd.ShowDialog() == DialogResult.OK) //用户点击确认按钮,发送确认消息
{
strFileName = ofd.FileName;//获取在文件对话框中选定的路径或者字符串
}
return strFileName;
}
public string OpenFolder()
{
string sPath="";
FolderBrowserDialog folder = new FolderBrowserDialog();
folder.Description="选择文件所在文件夹目录"; //定义在对话框上显示的文本
if (folder.ShowDialog()==DialogResult.OK)
{
sPath=folder.SelectedPath;
}
return sPath;
}
}
}