当前位置:网站首页>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
边栏推荐
- 沟通中经常用到的几个库存术语
- P5469 [noi2019] robot (Lagrange interpolation, interval DP)
- Three effective strategies for the transformation of data supply chain to be coordinated and successful
- 华裔科学家Ashe教授对涉嫌造假的Nature论文的正面回应
- 基本的SELECT语句
- C language dynamic memory management
- 8-其他编程语言--记录
- 每周招聘|PostgreSQL数据库研发工程师,年薪60+,名企高薪,挑战自我!
- 云原生微服务第一章之服务器环境说明
- Interview: your most impressive bug, for example
猜你喜欢

Import of MySQL data

为什么我还在CSDN写文章?一段陪伴学习的历程。

PostgreSQL and Navicat: the backbone of the database industry

MySQL 数据的导入
![[MySQL] CentOS 7.9 installation and use mysql-5.7.39 binary version](/img/70/5638080a2d2eabf6ae1f2a8db3abe6.png)
[MySQL] CentOS 7.9 installation and use mysql-5.7.39 binary version

Silicon Valley class lesson 7 - Tencent cloud on demand management module (2)

Practical project: boost search engine

逆袭黑马:数据库全栈工程师(DevDBOps)培训,把最好的课程送给您!
The memory occupation of the computer is too high after it is turned on (more than 50%)

Public cloud security and compliance considerations
随机推荐
SQL multi table query exercise
Counter attack dark horse: devdbops training, give you the best courses!
工作一年后,我有些感悟(写于2017年)
Disk expansion process and problems encountered in the virtual machine created by VMWare
每周招聘|PostgreSQL数据库研发工程师,年薪60+,名企高薪,挑战自我!
Problems and solutions encountered in using nextline(), nextint() and next() in scanner
Pre research of data quality management tools Griffin vs deequ vs great expectations vs quality
2019 biometric forum successfully ended: these ten highlights should not be missed!
Several inventory terms often used in communication
[Luogu] p2341 popular cattle
Basic use of gateway
Day07 MySQL knowledge points re summary and multi table query
HCIA-R&S自用笔记(21)STP技术背景、STP基础和数据包结构、STP选举规则及案例
Regular expressions and bypass case recurrence
电脑开机后内存占用过高(50%以上)
What is the reason for oom during redis shake synchronization in shake database?
研究阿尔茨海默病最经典的Nature论文涉嫌造假
kalibr标定realsenseD435i --多相机标定
Professor Ashe, a Chinese scientist, made a positive response to the suspected fake Nature paper
Dao:op token and non transferable NFT are committed to building a new digital democracy