脚本宝典收集整理的这篇文章主要介绍了Winform控件SaveFileDialog用于保存文件,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
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, Event@R_512_2604@s 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); } } } } }@H_406_14@
4、运行exe程序,在文本框中输入要保存的内容:
5、点击“保存文件”按钮,打开保存文件对话框,输入文件名,点击保存:
6、在Debug目录下面可以看到保存对话框.txt这个文件,打开文件,可以看到保存的内容:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本宝典。
以上是脚本宝典为你收集整理的Winform控件SaveFileDialog用于保存文件全部内容,希望文章能够帮你解决Winform控件SaveFileDialog用于保存文件所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。