当前位置:网站首页>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
边栏推荐
- HCIA-R&S自用笔记(20)VLAN综合实验、GVRP
- Differences between PHP round and sprintf functions
- Learn various details and thoughts of chatroom implementation in Muduo
- Lesson 2 of Silicon Valley classroom - building project environment and developing lecturer management interface
- 程序员成长第二十九篇:如何激励员工?
- Hcia-r & s self use notes (23) DHCP
- Will the approval in advance affect the formal approval?
- gateway基本使用
- 第二部分—C语言提高篇_9. 链表
- KT6368A蓝牙芯片开发注意事项以及问题集锦--长期更新
猜你喜欢

Database full stack Engineers (devdbops) have low down payment and high return, and pay after employment

An online accident, I suddenly realized the essence of asynchrony

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

Introduction to the use of Jerry downloader forced download tool_ Ac695n696nad14ad15 full range support

实战项目:Boost搜索引擎
What if redis memory is full? This is the right way to deal with it

逆袭黑马:数据库全栈工程师(DevDBOps)培训,把最好的课程送给您!

Kt6368a Bluetooth chip development precautions and problem collection - long term update

Is test development development development?

比海豹便宜,造型炸裂空间大,20万左右真没对手?长安全新“王炸”这样选才划算
随机推荐
[untitled]
数据供应链的转型 协调一致走向成功的三大有效策略
Three person management of system design
How to use data pipeline to realize test modernization
第二部分—C语言提高篇_9. 链表
Problems and solutions encountered in using nextline(), nextint() and next() in scanner
Xinding acquires Ziguang holdings! Wanye enterprise: comprehensive transformation of integrated circuits!
HCIA-R&S自用笔记(23)DHCP
Restful interface specification
Basic select statement
【flask高级】结合源码分析flask中的线程隔离机制
The nature and proof of the center of gravity of [mathematics] tree
org.yaml.snakeyaml.scanner. ScannerException: mapping values are not allowed here in ‘reader‘, line
Six challenges facing enterprise data governance!
HCIA-R&S自用笔记(18)园区网架构基础、交换机工作原理、VLAN原理
Weilai cup 2022 Niuke summer multi school training camp 2
Kt6368a Bluetooth chip development precautions and problem collection - long term update
Counter attack dark horse: devdbops training, give you the best courses!
Application of workflow engine in vivo marketing automation | engine 03
Dao:op token and non transferable NFT are committed to building a new digital democracy