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); } } } } }
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!