亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Winform控件SaveFileDialog用于保存文件

 更新時間:2017年03月07日 11:30:54   作者:.NET開發(fā)菜鳥  
這篇文章主要為大家詳細介紹了Winform SaveFileDialog保存文件對話框的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下

SaveFileDialog用于保存文件,供大家參考,具體內(nèi)容如下

1、新建Winform窗體應(yīng)用程序,命名為SaveFileDialogDemo。

2、在界面上添加一個按鈕的控件(用于打開保存文件對話框),添加文本控件,用于輸入要保存的內(nèi)容。

3、后臺代碼實現(xiàn):

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();
      //設(shè)置保存文件對話框的標(biāo)題
      sfd.Title = "請選擇要保存的文件路徑";
      //初始化保存目錄,默認exe文件目錄
      sfd.InitialDirectory = Application.StartupPath;
      //設(shè)置保存文件的類型
      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程序,在文本框中輸入要保存的內(nèi)容:

5、點擊“保存文件”按鈕,打開保存文件對話框,輸入文件名,點擊保存:

6、在Debug目錄下面可以看到保存對話框.txt這個文件,打開文件,可以看到保存的內(nèi)容:

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論