Home > Backend Development > C#.Net Tutorial > Winform control SaveFileDialog code example on how to save files

Winform control SaveFileDialog code example on how to save files

Y2J
Release: 2017-05-03 13:41:52
Original
3891 people have browsed it

This article mainly introduces the relevant information of Winform SaveFileDialog to save files in detail. It has certain reference value. Interested friends can refer to

SaveFileDialog is used to save files. For your reference, the specific content is as follows

1. Create a new Winform form application and name it SaveFileDialogDemo.

2. Add a button control on the interface (used to open the save file dialog box), and add a text control used to enter the content to be saved.

3. Background code implementation:

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);
        }
      }
    }
  }
}
Copy after login

4. Run the exe program and enter the content to be saved in the text box:

5. Click the "Save File" button to open the save file dialog box, enter the file name, and click Save:

6. In Debug You can see the save dialog box .txt file under the directory. Open the file and you can see the saved content:

The above is the detailed content of Winform control SaveFileDialog code example on how to save files. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template