1.按行读取:每次读取一行内容,即遇到回车键才会停止。
public void readfile(string filepath)
{
if (!File.Exists(filepath))
{
MessageBox.Show(" file not exits !");
return;
}
string[] result ;
FileStream fs = new FileStream(filepath, FileMode.Open);
using (StreamReader sr = new StreamReader(fs, Encoding.Default))
{
List<string> list = new List<string>();
string input;
while (!sr.EndOfStream)
{
input= sr.ReadLine().ToString();
}
}
sr.Close();
}
2. 理论上任意形式的读取方式
using System;
public class SplitTest {
public static void Main() {
string words = "This is a list of words, with: a bit of punctuation" +
"\tand a tab character.";
string [] split = words.Split(new Char [] {' ', ',', '.', ':', '\t' });
foreach (string s in split) {
if (s.Trim() != "")
Console.WriteLine(s);
}
}
}
// The example displays the following output to the console:
// This
// is
// a
// list
// of
// words
// with
// a
// bit
// of
// punctuation
// and
// a
// tab
// character