ASP.NET中有类似 asp 中 OpenTextFile的函数吗?

解决方案 »

  1.   

    呵呵,有。
    using(StreamReader sr = File.OpenText("文件路径"))
    {
       while(true)
       {
         string line = sr.ReadLine();
         if(line==null)break;
         //..处理line中的数据
       }
    }
      

  2.   

    using System;
    using System.IO;
    public class TextFromFile 
    {
        private const string FILE_NAME = "MyFile.txt";
        public static void Main(String[] args) 
        {
            if (!File.Exists(FILE_NAME)) 
            {
                Console.WriteLine("{0} does not exist.", FILE_NAME);
                return;
            }
            using (StreamReader sr = File.OpenText(FILE_NAME))
            {
                String input;
                while ((input=sr.ReadLine())!=null) 
                {
                    Console.WriteLine(input);
                }
                Console.WriteLine ("The end of the stream has been reached.");
                sr.Close();
            }
        }
      

  3.   

    有两点说明:
    1:文件路径必须是物理路径。对于ASP.NET来说你必须要映射一下地址;
    2:使用StreamReader循环从流中读取数据,这是一般的用法。你可以根据实际情况变动。当ReadLine返回null表示到达文件尾。