static void Main(string[] args)
        {
            StreamReader objReader = new StreamReader("c:\\test.txt",System.Text.Encoding.GetEncoding("gb2312"));
            string sLine="";
            ArrayList arrText = new ArrayList();
            while (sLine != null)
                {
                    sLine = objReader.ReadLine();
                    if (sLine != null)
                    arrText.Add(sLine);
                 }
            objReader.Close();
            foreach (string sOutput in arrText)
            Console.WriteLine(sOutput);
            Console.ReadLine();
        }
请教大家,以上这段代码能将文本框中的内容通过控制台输出,我想让他们在文本框中显示,应该怎么做呢?我在窗体上添加了名为source的文本框,并把最后一句改为source.Text = sOutput;可是行,请问我是错在哪了?请大侠执教!

解决方案 »

  1.   

    要修改项目属性中的输出类型,改成windows application.
      

  2.   

    错了,应该该成console application.
    然后我试了下,可以从控制台输入,文本框输出,代码如下
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;namespace Console_And_Form
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }        private void Form1_Load(object sender, EventArgs e)
            {
            }        private void button1_Click(object sender, EventArgs e)
            {
                string d = Console.ReadLine();
                textBox1.Text = d;
            }
        }
    }
      

  3.   

    我试了楼上的兄弟的代码无法运行啊,提示错误   命名空间“System”中不存在类型或命名空间名称“Linq”(是缺少程序集引用吗?)
      

  4.   

    谢谢楼上的兄弟,通过你的代码我明白了一些,请大家帮忙再看下,我在FORM上添加了一个文本框,一下是源代码,可以实现把内容读入到文本框,可是无法按原格式进行读入,读进来的是一大段内容,请问如何按原格式读入呢?
    using System;
    using System.IO;
    using System.Collections;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;namespace WindowsApplication4
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }        private void Form1_Load(object sender, EventArgs e)
            {
                StreamReader objReader = new StreamReader("c:\\test.txt", System.Text.Encoding.GetEncoding("gb2312"));
                string sLine = "";
                ArrayList arrText = new ArrayList();
                while (sLine != null)
                {
                    sLine = objReader.ReadLine();
                    if (sLine != null)
                        arrText.Add(sLine);
                }
                objReader.Close();
                foreach (string sOutput in arrText)
                textBox1.Text += sOutput;
            }
        }
    }
      

  5.   

    可以先读入到一个字符串,然后逐字搜索,TextBox进行append,遇到\n时,一起换行……这是个笨办法,我承认……
      

  6.   

    在你的代码中的最后一行,即:textBox1.Text   +=   sOutput  改成  textBox1.Text   +=   sOutput+"\n"就行了。
      

  7.   

    既然string sLine=" "了那sLine != null 在任何情况下都成立呀,null是代表未赋值而不是空值
      

  8.   

    谢谢Seanxiaoxiao和xxiecan 二位的帮助,我这回的分给少了,分别十分,非常感谢!