读文本文件
//实例化一个OpenFileDialog,用于打开文件选择器
OpenFileDialog openFileDialog = new OpenFileDialog();
if (openFileDialog.ShowDialog() == DialogResult.OK)//判断文件是否打开
{
string filename = openFileDialog.FileName; //文件路径+文件名,如C:/user/test.txt
//将文本文件中的内容以字符串数组全部读取,ReadAllLines()读取全部行
string[] str = System.IO.File.ReadAllLines(filename);
//string str=System.IO.File.ReadLines(filename); 读取一行
float[] a = new float[str.Length];
float[] b = new float[str.Length];
for (int i = 0; i < str.Length; i++)
{
//将文本文件单行内容(字符串)根据分隔符拆分成字符串数组
string[] dstr = str[i].Split(" ,;\t".ToArray(),StringSplitOptions.RemoveEmptyEntries);
a[i] = float.Parse(dstr[0]);
b[i] = float.Parse(dstr[1]);
}
}
除了用System.IO.File读取还可以用文件流的方式读取。
FileStream fs = new FileStream(fileName, FileM