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这个文件,翻开文件,能够看到保留的内容:
以上就是Winform控件SaveFileDialog关于保留文件用法的代码示例的细致内容,更多请关注ki4网别的相干文章!