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;
}
}
}
C#打开文件、文件夹对话框
最新推荐文章于 2025-08-16 09:06:45 发布
本文介绍了一个简单的C#程序,该程序利用System.Windows.Forms命名空间下的OpenFileDialog和FolderBrowserDialog类来实现文件和文件夹的选择功能。通过设置过滤器和验证选项,程序能够确保用户选择符合要求的文件或文件夹。
4501

被折叠的 条评论
为什么被折叠?



