| 发表于:2008-01-23 16:11:06 楼主 |
大侠们,请问一下怎么在文本框读入一个文本文件,并把文本文件的每一行分别存到一个数组里? 为什么我这样做不 行?文本框2里面的内容少了一部分。 代码如下: openfiledialog aa = new openfiledialog(); aa.filter = "text file(*.txt) ¦*.txt ¦all files(*.*) ¦*.*"; aa.filterindex = 1; aa.restoredirectory = true; streamreader sr = null; string[] pp=new string[50]; string line=null; if (aa.showdialog() == dialogresult.ok) { //读取文本文件到 textbox1里。 sr = new streamreader(aa.filename, system.text.encoding.default); textbox1.text = sr.readtoend(); //按行读取到数组pp里。 sr = new streamreader(aa.filename, system.text.encoding.default); line = sr.readline(); int i = 0; while ((line = sr.readline()) != null) { pp[i] = sr.readline(); i++; } } sr.close(); //把字符串数组里的字符串多行显示到文本框里。 string lf = null; for(int m=0;m <50;m++) { lf = lf + pp[m]+"\r\n"; } textbox2.text = lf; 显示结果如下: textbox1内容: 我 是 谁 ? y x 0 1 2 3 4 5 textbox2内容: 谁 y 0 2 4 6 8 |
|
|
|
|