16.WinForm練習--保存對話框

namespace _16.保存對話框
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

    private void button1_Click(object sender, EventArgs e)
    {
        //打開保存對話框
        SaveFileDialog sfd = new SaveFileDialog();
        //設置對話框屬性
        sfd.Title = "請選擇保存文件的路徑";
        sfd.InitialDirectory = @"C:\Users\Administrator.USER-20180925HC\Desktop\pic";
        sfd.Filter = "文本文件|*.txt|所有文件|*.*";
        sfd.ShowDialog();

        //獲得保存文件的路徑
        string path = sfd.FileName;
        if (path == "")
        {
            return;
        }
        using(FileStream fsWrite=new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write))
        {
            byte[] buffer = Encoding.Default.GetBytes(textBox1.Text);
            fsWrite.Write(buffer, 0, buffer.Length);
        }
        MessageBox.Show("保存成功");

    }
}

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章