C# SaveFileDialog与OpenFileDialog用法案例详解
作者:Fendouche 时间:2023-01-28 05:44:42
1.OpenFileDialog
private void btnOpen_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.InitialDirectory = @"C:\Users\LWP1398\Desktop"; //设置初始路径
ofd.Filter = "Excel文件(*.xls)|*.xls|Csv文件(*.csv)|*.csv|所有文件(*.*)|*.*"; //设置“另存为文件类型”或“文件类型”框中出现的选择内容
ofd.FilterIndex = 2; //设置默认显示文件类型为Csv文件(*.csv)|*.csv
ofd.Title = "打开文件"; //获取或设置文件对话框标题
ofd.RestoreDirectory = true;
if (ofd.ShowDialog() == DialogResult.OK)
{
//FileName:所选文件的全路径 SafeFileName:所选的文件名
txtPath.Text = "FileName:" + ofd.FileName + "\r\n" + "SafeFileName:" + ofd.SafeFileName;
}
}
2.OpenFileDialog选择多个文件
private void button3_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.InitialDirectory = @"C:\Users\LWP1398\Desktop"; //设置初始路径
ofd.Filter = "Excel文件(*.xls)|*.xls|Csv文件(*.csv)|*.csv|所有文件(*.*)|*.*"; //设置“另存为文件类型”或“文件类型”框中出现的选择内容
ofd.FilterIndex = 2; //设置默认显示文件类型为Csv文件(*.csv)|*.csv
ofd.Title = "打开文件"; //获取或设置文件对话框标题
ofd.RestoreDirectory = true;设置对话框是否记忆上次打开的目录
ofd.Multiselect = true;//设置多选
if (ofd.ShowDialog() == DialogResult.OK)
{
for (int i = 0; i < ofd.FileNames.Length; i++)
{
txtPath.Text += ofd.FileNames[i] + "\r\n";//输出一个路径回车换行
}
for (int i = 0; i < ofd.FileNames.Length; i++)
{
txtPath.Text += ofd.SafeFileNames[i] + "\r\n";
}
}
}
3.SaveFileDialog
private void button2_Click(object sender, EventArgs e)
{
SaveFileDialog sfd=new SaveFileDialog();
sfd.Filter = "文本文件(*.txt)|*.txt|所有文件|*.*";//设置文件类型
sfd.FileName = "保存";//设置默认文件名
sfd.DefaultExt = "txt";//设置默认格式(可以不设)
sfd.AddExtension = true;//设置自动在文件名中添加扩展名
if (sfd.ShowDialog()==DialogResult.OK)
{
txtPath.Text = "FileName:" + sfd.FileName + "\r\n" ;
using (StreamWriter sw = new StreamWriter(sfd.FileName))
{
sw.WriteLineAsync("今天是个好天气");
}
}
MessageBox.Show("ok");
}
private void saveFileDialog1_FileOk(object sender, CancelEventArgs e)
{
saveFileDialog1.AddExtension = true; //自动添加扩展名
e.Cancel = true; //取消保存操作
string 扩展名 = System.IO.Path.GetExtension(saveFileDialog1.FileName);
//判断扩展名并实现自定义的保存操作(导出)
if (扩展名 == "txt")
{ }
if (扩展名 == "xml")
{ }
}
4.FolderBrowserDialog
string defaultPath = "";
FolderBrowserDialog dialog = new FolderBrowserDialog();
//打开的文件夹浏览对话框上的描述
dialog.Description = "请选择一个文件夹";
//是否显示对话框左下角 新建文件夹 按钮,默认为 true
dialog.ShowNewFolderButton = false;
//首次defaultPath为空,按FolderBrowserDialog默认设置(即桌面)选择
if (defaultPath != "")
{
//设置此次默认目录为上一次选中目录
dialog.SelectedPath = defaultPath;
}
//按下确定选择的按钮
if (dialog.ShowDialog() == DialogResult.OK)
{
//记录选中的目录
defaultPath = dialog.SelectedPath;
}
MessageBox.show(defaultPath);
来源:https://blog.csdn.net/u011108093/article/details/81627935
标签:C#,SaveFileDialog,OpenFileDialog
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
关于eclipse中运行tomcat提示端口被占用的4种解决
2022-04-15 10:56:12
Java_异常类(错误和异常,两者的区别介绍)
2023-09-19 08:53:27
![](https://img.aspxhome.com/file/2023/1/58311_0s.jpg)
Java在指定路径上创建文件提示不存在解决方法
2021-07-31 10:06:21
java微信企业号开发之通讯录
2022-04-28 12:51:40
![](https://img.aspxhome.com/file/2023/1/71911_0s.png)
c#使用xamarin编写拨打电话程序
2023-09-04 18:09:20
android使用Socket通信实现多人聊天应用
2023-07-08 02:43:37
![](https://img.aspxhome.com/file/2023/1/138071_0s.png)
基于WPF实现验证码控件
2021-08-15 21:44:36
![](https://img.aspxhome.com/file/2023/5/92975_0s.png)
Java读取txt文件中的数据赋给String变量方法
2022-08-04 22:32:19
深入理解C++中public、protected及private用法
2023-07-02 11:30:17
C#中委托用法实例详解
2022-11-02 23:24:40
Android自定义ViewPagerIndicator实现炫酷导航栏指示器(ViewPager+Fragment)
2021-11-05 13:16:12
![](https://img.aspxhome.com/file/2023/6/130396_0s.gif)
Java Pattern和Matcher字符匹配方式
2022-06-07 21:57:56
![](https://img.aspxhome.com/file/2023/5/93075_0s.png)
@Configuration与@Component作为配置类的区别详解
2023-03-09 19:50:15
java线程池合理设置最大线程数和核心线程数方式
2021-06-19 22:02:31
idea的使用之关于tomcat热部署的教程
2022-12-02 20:16:46
![](https://img.aspxhome.com/file/2023/3/129953_0s.png)
unity 实现摄像机绕某点旋转一周
2021-06-11 16:48:57
Android端代码量非常小的分页加载库
2022-08-25 17:24:48
![](https://img.aspxhome.com/file/2023/9/137349_0s.gif)
Java细数IO流底层原理到方法使用
2022-08-06 21:30:45
![](https://img.aspxhome.com/file/2023/1/96071_0s.png)
Android 中使用RadioGroup和Fragment实现底部导航栏的功能
2022-07-17 16:11:04
![](https://img.aspxhome.com/file/2023/3/106013_0s.gif)
SpringBoot SSO轻松实现(附demo)
2022-04-05 02:24:33