首页 > Windows开发 > 详细

Winform控件:保存文件对话框(SaveFileDialog)

时间:2017-02-07 12:09:19      阅读:233      评论:0      收藏:0      [点我收藏+]

SaveFileDialog用于保存文件

1、新建Winform窗体应用程序,命名为SaveFileDialogDemo。

2、在界面上添加一个按钮的控件(用于打开保存文件对话框),添加文本控件,用于输入要保存的内容。

技术分享

3、后台代码实现:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.ComponentModel;
 4 using System.Data;
 5 using System.Drawing;
 6 using System.IO;
 7 using System.Linq;
 8 using System.Text;
 9 using System.Threading.Tasks;
10 using System.Windows.Forms;
11 
12 namespace SaveFileDialogDemo
13 {
14     public partial class Form1 : Form
15     {
16         public Form1()
17         {
18             InitializeComponent();
19         }
20 
21         /// <summary>
22         /// 保存文件按钮
23         /// </summary>
24         /// <param name="sender"></param>
25         /// <param name="e"></param>
26         private void btn_SaveFile_Click(object sender, EventArgs e)
27         {
28             //
29             SaveFileDialog sfd = new SaveFileDialog();
30             //设置保存文件对话框的标题
31             sfd.Title = "请选择要保存的文件路径";
32             //初始化保存目录,默认exe文件目录
33             sfd.InitialDirectory = Application.StartupPath;
34             //设置保存文件的类型
35             sfd.Filter = "文本文件|*.txt|音频文件|*.wav|图片文件|*.jpg|所有文件|*.*";
36             if (sfd.ShowDialog() == DialogResult.OK)
37             { 
38                 //获得保存文件的路径
39                 string filePath = sfd.FileName;
40                 //保存
41                 using (FileStream fsWrite = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.Write))
42                 {
43                     byte[] buffer = Encoding.Default.GetBytes(txt_FileInfo.Text.ToString().Trim());
44                     fsWrite.Write(buffer, 0, buffer.Length);
45                 }
46             }
47         }
48     }
49 }

4、运行exe程序,在文本框中输入要保存的内容:

技术分享

5、点击“保存文件”按钮,打开保存文件对话框,输入文件名,点击保存:

技术分享

6、在Debug目录下面可以看到保存对话框.txt这个文件,打开文件,可以看到保存的内容:

技术分享

Winform控件:保存文件对话框(SaveFileDialog)

原文:http://www.cnblogs.com/dotnet261010/p/6373222.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!