110,538
社区成员
发帖
与我相关
我的任务
分享
private void ReadFile(string filepath)
{
try
{
FileStream aFile = new FileStream(filepath, FileMode.Open);
StreamReader sr = new StreamReader(aFile, Encoding.Default);
ArrayList arrText = new ArrayList();
/*while (sr.Peek() != -1)
{
string strLine = sr.ReadLine();
arrText.Add(strLine);
}*/
arrText.Add(sr.ReadToEnd()); //作用跟上面的注释语句基本等效,不过我觉得这个更好。
string[] arrString = new string[arrText.Count];
arrText.CopyTo(arrString, 0);
txtTextArea.Lines = arrString;
sr.Close();
}
catch (Exception exc)
{
MessageBox.Show(exc.Message, "打开文件失败!");
}
}
private void SaveFile(string filepath)
{
FileStream aFile = new FileStream(filepath, FileMode.OpenOrCreate);
StreamWriter sw = new StreamWriter(aFile, Encoding.Default);
try
{
foreach (string oneLine in txtTextArea.Lines)
sw.WriteLine(oneLine);
//这段注释代码跟上面的foreach语句作用等效
/*int i;
for (i = 0; i < txtTextArea.Lines.Length - 1; i++)
{
sw.WriteLine(txtTextArea.Lines[i]);
}*/
sw.Close();
}
catch (Exception exc)
{
MessageBox.Show(exc.Message, "保存文件出错!");
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.openFileDialog1.ShowDialog();
}
private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
{
StreamReader hc = new StreamReader(this.openFileDialog1.FileName.ToString());
this.richTextBox1.Text = hc.ReadToEnd();
}
}
}