1. 程式人生 > >按行讀取文字中的內容

按行讀取文字中的內容

實現效果:

  

知識運用:

  StreamReader類的ReadLine方法

實現程式碼:

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog open = new OpenFileDialog();
            string strLine = null;
            open.Filter = "文字檔案(*.txt)|*.txt";
            open.ShowDialog();
            textBox1.Text = open.FileName;
            StreamReader st=new StreamReader(textBox1.Text);
            while((strLine=st.ReadLine())!=null)
            {
                textBox2.Text += strLine + Environment.NewLine;
            }
        }