小弟在编写c#程序时需要创建一个文本文件,以便对其进行读取,但是编译总不行,我是这样写的:
..........
.........
.......    Tom.txt:
      Tiger
      Monkey
前面都是代码,中间是我想添加的文本文件.我单独把它写在程序中了,老是通不过,请高手指点,谢谢!

解决方案 »

  1.   

    using System;
    using System.IO;class Test 
    {
        public static void Main() 
        {
            string path = @"c:\temp\MyTest.txt";
            if (!File.Exists(path)) 
            {
                // Create a file to write to.
                using (StreamWriter sw = File.CreateText(path)) 
                {
                    sw.WriteLine("Hello");
                    sw.WriteLine("And");
                    sw.WriteLine("Welcome");
                }    
            }        // Open the file to read from.
            using (StreamReader sr = File.OpenText(path)) 
            {
                string s = "";
                while ((s = sr.ReadLine()) != null) 
                {
                    Console.WriteLine(s);
                }
            }
        }
    }
    ms-help://MS.MSDNQTR.2003FEB.2052/cpref/html/frlrfSystemIOFileClassCreateTextTopic.htm
      

  2.   

    多谢楼上的,我这个程序是一个win窗体程序,要把这个文本文件建立以后,可以从组合框comobox直接读取这个文件的内容使用,书上说就是简单的两行创建文本:  occupationgs.txt
                                                                consultant
                                                                programmer
     就可以,所以我调试老是通不过.就按照我的意思应该怎么来写它,请高手再次帮忙