StreamWriter不关闭,将不会写入到文件中
using System.Collections;
using System.Text;
namespace 字典排序
{
public partial class Form1 : Form
{
string file = "";
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
progressBar1.Value = 0;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
file = openFileDialog1.FileName;
label1.Text = file;
}
}
private void button2_Click(object sender, EventArgs e)
{
if (File.Exists(file))
{
FileStream fs = new FileStream(file, FileMode.Open);
StreamReader sr = new StreamReader(fs);
List
gg = new List
();
String sb;
while ((sb = sr.ReadLine()) != null)
{
gg.Add(sb);
}
gg.Sort();
sr.Close();
fs.Close();
progressBar1.Value = 50;
fs = new FileStream(file, FileMode.Create);
StreamWriter sw = new StreamWriter(fs);
foreach (object obj in gg)
{
sw.WriteLine(obj.ToString());
}
sw.Close();
fs.Close();
progressBar1.Value = 100;
MessageBox.Show("Finished");
}
else
{
label1.Text = "";
MessageBox.Show("请选择文本文档!");
}
}
private void Form1_Load(object sender, EventArgs e)
{
label1.Text = "";
}
}
}