当前位置:网站首页>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
边栏推荐
- KT6368A蓝牙芯片开发注意事项以及问题集锦--长期更新
- Go uses flag package to parse command line parameters
- Hcia-r & s self use notes (23) DHCP
- SQL Basics
- How to use data pipeline to realize test modernization
- Problems and solutions encountered in using nextline(), nextint() and next() in scanner
- Interview: your most impressive bug, for example
- HCIA-R&S自用笔记(23)DHCP
- 【flask高级】结合源码分析flask中的线程隔离机制
- SQL 基础知识
猜你喜欢

json格式化小工具--pyqt5实例

How to recover the original data when the U disk is damaged, and how to recover the damaged data when the U disk is damaged

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

HCIA-R&S自用笔记(19)VLAN配置及实验、VLAN间路由

Recruit | PostgreSQL database R & D engineers every week, with an annual salary of 60+, high salary for famous enterprises, and challenge yourself!
![[shaders realize distorted outline effect _shader effect Chapter 2]](/img/b3/ab28039cce2521ff1d59f0de6bf852.png)
[shaders realize distorted outline effect _shader effect Chapter 2]

Kt6368a Bluetooth chip development precautions and problem collection - long term update
![[untitled]](/img/88/68952146da17a4a504ee7d1bc8858d.png)
[untitled]

1. Configuration environment and project creation

MySQL random paging to get non duplicate data
随机推荐
Regular expressions and bypass case recurrence
数据供应链的转型 协调一致走向成功的三大有效策略
8 other programming languages -- Recording
Huawei atlas900 reveals the secret: it integrates thousands of shengteng 910 chips, and its computing power is comparable to 500000 PCs!
What if redis memory is full? This is the right way to deal with it
With a total investment of 10billion US dollars, Huahong Wuxi 12 inch wafer factory was officially put into operation
P5469 [NOI2019] 机器人(拉格朗日插值、区间dp)
Pre research of data quality management tools Griffin vs deequ vs great expectations vs quality
30、 Modern storage system (management database and distributed storage system)
Silicon Valley class lesson 7 - Tencent cloud on demand management module (2)
json格式化小工具--pyqt5实例
杰理下载器强制下载工具的使用介绍_AC695N696NAD14AD15全系列支持
PostgreSQL and Navicat: the backbone of the database industry
程序员成长第二十九篇:如何激励员工?
HCIA-R&S自用笔记(23)DHCP
蔚来杯2022牛客暑期多校训练营2
Making wechat robot with go (I) sending messages
HCIA-R&S自用笔记(21)STP技术背景、STP基础和数据包结构、STP选举规则及案例
Reduce power consumption and upgrade functions! Qiyingtailun released the second generation voice AI chip: the module price is as low as 14.99 yuan!
HCIA-R&S自用笔记(19)VLAN配置及实验、VLAN间路由