C#通用对话框(打开,保存)

来源:互联网 发布:网络销售协议 编辑:程序博客网 时间:2024/05/28 04:55
namespace WindowsFormsApplication1{    partial class Form1    {        /// <summary>        /// 必需的设计器变量。        /// </summary>        private System.ComponentModel.IContainer components = null;        /// <summary>        /// 清理所有正在使用的资源。        /// </summary>        /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>        protected override void Dispose(bool disposing)        {            if (disposing && (components != null))            {                components.Dispose();            }            base.Dispose(disposing);        }        #region Windows 窗体设计器生成的代码        /// <summary>        /// 设计器支持所需的方法 - 不要        /// 使用代码编辑器修改此方法的内容。        /// </summary>        private void InitializeComponent()        {            this.button1 = new System.Windows.Forms.Button();            this.button2 = new System.Windows.Forms.Button();            this.button3 = new System.Windows.Forms.Button();            this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();            this.saveFileDialog1 = new System.Windows.Forms.SaveFileDialog();            this.richTextBox1 = new System.Windows.Forms.RichTextBox();            this.SuspendLayout();            //             // button1            //             this.button1.Location = new System.Drawing.Point(449, 71);            this.button1.Name = "button1";            this.button1.Size = new System.Drawing.Size(75, 23);            this.button1.TabIndex = 0;            this.button1.Text = "button1";            this.button1.UseVisualStyleBackColor = true;            this.button1.Click += new System.EventHandler(this.button1_Click);            //             // button2            //             this.button2.Location = new System.Drawing.Point(449, 136);            this.button2.Name = "button2";            this.button2.Size = new System.Drawing.Size(75, 23);            this.button2.TabIndex = 1;            this.button2.Text = "button2";            this.button2.UseVisualStyleBackColor = true;            this.button2.Click += new System.EventHandler(this.button2_Click);            //             // button3            //             this.button3.Location = new System.Drawing.Point(449, 207);            this.button3.Name = "button3";            this.button3.Size = new System.Drawing.Size(75, 23);            this.button3.TabIndex = 2;            this.button3.Text = "button3";            this.button3.UseVisualStyleBackColor = true;            this.button3.Click += new System.EventHandler(this.button3_Click);            //             // openFileDialog1            //             this.openFileDialog1.FileName = "openFileDialog1";            //             // richTextBox1            //             this.richTextBox1.Location = new System.Drawing.Point(61, 71);            this.richTextBox1.Name = "richTextBox1";            this.richTextBox1.Size = new System.Drawing.Size(362, 159);            this.richTextBox1.TabIndex = 3;            this.richTextBox1.Text = "";            //             // Form1            //             this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;            this.ClientSize = new System.Drawing.Size(575, 339);            this.Controls.Add(this.richTextBox1);            this.Controls.Add(this.button3);            this.Controls.Add(this.button2);            this.Controls.Add(this.button1);            this.Name = "Form1";            this.Text = "Form1";            this.Load += new System.EventHandler(this.Form1_Load);            this.ResumeLayout(false);        }        #endregion        private System.Windows.Forms.Button button1;        private System.Windows.Forms.Button button2;        private System.Windows.Forms.Button button3;        private System.Windows.Forms.OpenFileDialog openFileDialog1;        private System.Windows.Forms.SaveFileDialog saveFileDialog1;        private System.Windows.Forms.RichTextBox richTextBox1;    }}

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;using System.IO;namespace WindowsFormsApplication1{    public partial class Form1 : Form    {        public Form1()        {            InitializeComponent();        }        private void Form1_Load(object sender, EventArgs e)        {            button1.Text = "打开";            button2.Text = "保存";            button3.Text = "关闭";        }        private void button1_Click(object sender, EventArgs e)        {            this.openFileDialog1.InitialDirectory = @"F;\";            this.openFileDialog1.Title = "打开文件";            this.openFileDialog1.ShowHelp = true;            this.openFileDialog1.Filter = "*.txt|*.txt";            this.openFileDialog1.ShowDialog();            string file = this.openFileDialog1.FileName;            if (string.IsNullOrEmpty(file)) return;            //以下为写字符到文本文件,需要添加System.IO引用            //创建一个文件流            FileStream fs = new FileStream(file, FileMode.Open,                FileAccess.Read);            //创建一个StreamWriter对象            StreamReader sr = new StreamReader(fs);            this.richTextBox1.Text = sr.ReadToEnd();            //释放StreamWriter对象,文件流对象            sr.Dispose();        }        private void button2_Click(object sender, EventArgs e)        {            this.saveFileDialog1.Filter = "*.txt|*.txt";            this.saveFileDialog1.ShowDialog();            string file = this.saveFileDialog1.FileName;            if (string.IsNullOrEmpty(file)) return;            //以下为写字符到文本文件,需要添加System.IO引用            //创建一个文件流            FileStream fs = new FileStream(file, FileMode.OpenOrCreate,                FileAccess.Write);            //创建一个StreamWriter对象            StreamWriter sw = new StreamWriter(fs);            sw.Write(this.richTextBox1.Text);            //释放StreamWriter对象,文件流对象            sw.Dispose();            fs.Dispose();        }        private void button3_Click(object sender, EventArgs e)        {            DialogResult result = MessageBox.Show("确定要结束程序吗?[Yes|No]", "提示",                MessageBoxButtons.YesNo, MessageBoxIcon.Warning,                MessageBoxDefaultButton.Button2);            if (result == DialogResult.Yes)            {                Application.Exit();            }        }    }}

0 0
原创粉丝点击