当前位置:网站首页>18、打开、保存文件对话框使用小记
18、打开、保存文件对话框使用小记
2022-07-26 22:20:00 【komla168】
前言:打开对话框、保存对话框对于桌面软件使用的还是比较频繁的,但是笔者最近使用发现同名API存在不同的命名空间下,现记录下。
一、官网API
1.1 System.Windows.Forms
OpenFileDialog和SaveFileDialog这两个对话框最早出现在System.Windows.Forms命名空间下,随着.Net Framwork1.1版本就出现了。而且它们还引用了System.ComponentModel命名空间。


1.2 Microsoft.Win32
而位于Microsoft.Win32下的OpenFileDialog和SaveFileDialog函数则是在.Net Framwork3.0才出现的,而Microsoft.Win32应用程序编程接口(API)是用于win32的一系列接口开发程序。


二、用法
用法差不多,只要引用对应的命名空间就可以,笔者在使用时遇见的一点不同是ShowDialog()的返回类型。


引用的命名空间是Microsoft.Win32
/// <summary>
/// 220723 komla 文件选择对话框
/// </summary>
void SelectFileDialog()
{
OpenFileDialog fie = new OpenFileDialog();
//创建对象
fie.Title = "上传头像";
//设置文本框标题
fie.InitialDirectory = @"C:\Users\Administrator\Desktop";
//对话框的初始目录
fie.Filter = "图像文件|*.jpg*|图像文件|*.png|所有文件|*.*";
//设置文件类型
string str = fie.FileName;
//获取选择的路径
if (fie.ShowDialog() == true)
{
MessageBox.Show(fie.FileName);
//用户点击 打开 后要执灯的代码
}
}
/// <summary>
/// 220723 komla 文件保存对话框
/// </summary>
void SaveFileDialog()
{
SaveFileDialog s = new SaveFileDialog();
//创建对象
s.Filter = "图像文件|*.jpg|图像文件|*.png|所有文件|*.*";
//保存的文件扩展名
s.Title = "保存文件";
//对话框标题
s.DefaultExt = "图像文件|*.jpg";
//设置文件默认扩展名
// string str = s.DefaultExt;
//获取文件默认件扩展名
s.InitialDirectory = @"C:\Users\Administrator\Desktop";
//设置保存的初始目录
if (s.ShowDialog() == true)
{
//用户点击"保存"后执行的代码
}
string str = s.FileName;
//获取用户保存的目录
}如果引用System.Windows.Forms命名空间的话只需要将ShowDialog()的值改为DialogResult.OK
if (s.ShowDialog() == DialogResult.OK)
{
//用户点击"保存"后执行的代码
}三、引用文献
3.1 C# “文件选择对话框“ 和 “保存对话框“_生产队的驴.的博客-CSDN博客_c# 选择文件对话框
3.2 OpenFileDialog 类 (System.Windows.Forms) | Microsoft Docs
边栏推荐
- Pre research of data quality management tools Griffin vs deequ vs great expectations vs quality
- C.Net timestamp and time conversion support time zone
- MySQL syntax uses detailed code version
- 研究阿尔茨海默病最经典的Nature论文涉嫌造假
- Silicon Valley class lesson 5 - Tencent cloud object storage and course classification management
- Vector execution engine framework gluten announced the official open source and appeared at spark technology summit
- Custom type
- The secret weapon of apple iphone11 series: U1 chip may usher in the era of ultra wideband
- json格式化小工具--pyqt5实例
- [flask advanced] analyze the thread isolation mechanism in flask in combination with the source code
猜你喜欢

PostgreSQL 与 Navicat:数据库行业的中坚力量
电脑开机后内存占用过高(50%以上)

Silicon Valley class lesson 5 - Tencent cloud object storage and course classification management

Science | 华盛顿大学利用AI和结构预测设计全新蛋白质

Download win10 system image and create virtual machine on VMware virtual machine
![[shaders realize distorted outline effect _shader effect Chapter 2]](/img/b3/ab28039cce2521ff1d59f0de6bf852.png)
[shaders realize distorted outline effect _shader effect Chapter 2]

Vit:vision transformer super detailed with code

Practical project: boost search engine

Disk expansion process and problems encountered in the virtual machine created by VMWare

Counter attack dark horse: devdbops training, give you the best courses!
随机推荐
企业数据治理面临的六大挑战!
工作一年后,我有些感悟(写于2017年)
Several inventory terms often used in communication
逆袭黑马:数据库全栈工程师(DevDBOps)培训,把最好的课程送给您!
json格式化小工具--pyqt5实例
[flask advanced] analyze the thread isolation mechanism in flask in combination with the source code
Eureka基本使用
面试官问:JS的this指向
[MySQL] CentOS 7.9 installation and use mysql-5.7.39 binary version
DAO:OP 代币和不可转让的 NFT 致力于建立新的数字民主
Kingbasees SQL language reference manual of Jincang database (3.1.1.14. scope type)
Custom type
[shader realizes shine effect _shader effect Chapter 3]
HCIA-R&S自用笔记(21)STP技术背景、STP基础和数据包结构、STP选举规则及案例
Typescript stage learning
P5469 [noi2019] robot (Lagrange interpolation, interval DP)
Counter attack dark horse: devdbops training, give you the best courses!
PostgreSQL and Navicat: the backbone of the database industry
An online accident, I suddenly realized the essence of asynchrony
第二部分—C语言提高篇_9. 链表