SaveFileDialog用于保存文件,供大家参考,具体内容如下
1、新建Winform窗体应用程序,命名为SaveFileDialogDemo。
2、在界面上添加一个按钮的控件(用于打开保存文件对话框),添加文本控件,用于输入要保存的内容。
3、后台代码实现:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace SaveFileDialogDemo { public partial class Form1 : Form { public Form1() { InitializeComponent(); } /// <summary> /// 保存文件按钮 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btn_SaveFile_Click(object sender, EventArgs e) { // SaveFileDialog sfd = new SaveFileDialog(); //设置保存文件对话框的标题 sfd.Title = "请选择要保存的文件路径"; //初始化保存目录,默认exe文件目录 sfd.InitialDirectory = Application.StartupPath; //设置保存文件的类型 sfd.Filter = "文本文件|*.txt|音频文件|*.wav|图片文件|*.jpg|所有文件|*.*"; if (sfd.ShowDialog() == DialogResult.OK) { //获得保存文件的路径 string filePath = sfd.FileName; //保存 using (FileStream fsWrite = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.Write)) { byte[] buffer = Encoding.Default.GetBytes(txt_FileInfo.Text.ToString().Trim()); fsWrite.Write(buffer, 0, buffer.Length); } } } } }
4、运行exe程序,在文本框中输入要保存的内容:
5、点击“保存文件”按钮,打开保存文件对话框,输入文件名,点击保存:
6、在Debug目录下面可以看到保存对话框.txt这个文件,打开文件,可以看到保存的内容:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持华域联盟。
您可能感兴趣的文章:
- C#日期控件datetimepicker保存空值的三种方法
- winform dateTime数据类型转换方法
- c# Winform自定义控件-仪表盘功能
- Winform控件Picture实现图片拖拽显示效果
- WinForm IP地址输入框控件实现
- WinForm实现鼠标拖动控件跟随效果
- winform实现可拖动的自定义Label控件
- 使用重绘项美化WinForm的控件
- C# winform自定义翻页控件详解
- C# WinForm实现窗体上控件自由拖动功能示例
- C#中WinForm控件的拖动和缩放的实现代码
- C# WinForm-Timer控件的使用
本文由 华域联盟 原创撰写:华域联盟 » Winform控件SaveFileDialog用于保存文件
转载请保留出处和原文链接:https://www.cnhackhy.com/76455.htm