123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- 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 WindowsFormsIOFileStream
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
-
- private void button1_Click(object sender, EventArgs e)
- {
-
- if (textBox1.Text == string.Empty)
- {
- MessageBox.Show("要写入的文件内容不能为空");
- }
- else
- {
-
- saveFileDialog1.Filter = "文本文件(*.txt)|*.txt";
- if (saveFileDialog1.ShowDialog() == DialogResult.OK)
- {
-
- StreamWriter sw = new StreamWriter(saveFileDialog1.FileName, true);
-
- sw.WriteLine(textBox1.Text);
-
- sw.Close();
- textBox1.Text = string.Empty;
- }
- }
- }
-
- private void button2_Click(object sender, EventArgs e)
- {
-
- openFileDialog1.Filter = "文本文件(*.txt)|*.txt";
- if (openFileDialog1.ShowDialog() == DialogResult.OK)
- {
- textBox1.Text = string.Empty;
-
- StreamReader sr = new StreamReader(openFileDialog1.FileName);
-
- textBox1.Text = sr.ReadToEnd();
-
- sr.Close();
- }
- }
- }
- }
|