using System;
using System.IO;class Test 
{
public static void Main() 
{
try 
{
using (StreamReader sr = new StreamReader("TestFile.txt",System.Text.Encoding.Default)) 
{
String line;
while ((line = sr.ReadLine()) != null) 
{
Console.WriteLine(line);
}
}
}
catch (Exception e) 
{
Console.WriteLine("The file could not be read:");
Console.WriteLine(e.Message);
}
}
}

解决方案 »

  1.   

    using System.IO;
    ..............................
    StreamReader sr = File.OpenText(@"c:\test.txt"); 
    string s = sr.ReadLine();  
    //hope it helps
      

  2.   

    using System.IO;//必须
    StreamReader Sr=File.OpenText([文件名]);
    string s=Sr.ReadLine();
      

  3.   

    在msdn中查找System.IO,你会有惊喜发现   :)
      

  4.   

    1,全文读取,上面说得差不多了,还要注意的就是字符集的问题,如果你的txt是ascii编码的话,就要注意了,一定要指明字符集StreamReader sr = new StreamReader(@"c:\test1.txt",System.Text.Encoding.GetEncoding("GB2312"));
    2,如果你指的是读取有格式的ini文件,可以使用GetPrivateProfile* 系列API
    /// <summary>
    /// 从配置文件中取得数据库连接字符串 
    /// 默认:Web方式,从Web.config 中取 GetConnStrWeb()
    /// 桌面方式:从Windows目录下的eii.ini 中取 GetConnStrDeskTop()
    /// </summary>
    public  class SQLConnString
    {
    private const string USER = "User id";
    private const string PASS = "password";
    private const string SOURCE = "data source";
    private const string CATALOG= "catalog";
    private const string SIZE = "size"; private const string SECTION= "DataBase";
    private static string INI_FILE= System.Environment.GetEnvironmentVariable("WINDIR")+"\\eii.ini";
    // @"C:\Winnt\system32\ini.dll"; [DllImport("kernel32")]
    private static extern int GetPrivateProfileString(string section,string key,string def,StringBuilder retVal,int size,string filePath);
    [DllImport("kernel32")]
    private static extern uint GetPrivateProfileInt(string section,string key,uint size,string filePath);
    public SQLConnString()
    {
    //
    // TODO: 在此处添加构造函数逻辑
    //
    }

    /// <summary>
    /// 取得web程序的数据库连接字符串
    /// </summary>
    /// <returns></returns>
    private static string GetConnStrWeb()
    {
    return ConfigurationSettings.AppSettings["Tiny_Dust_DL_DataBase_Conn_String"];
    } /// <summary>
    /// 取得CS程序的连接字符串
    /// </summary>
    /// <returns></returns>
    private static string GetConnStrDeskTop()
    {
    string ReturnText;
    ReturnText="persist security info=False; packet size="+GetPrivateProfileInt(SECTION,SIZE,4096,INI_FILE).ToString()+";data source="+IniReadValue(SOURCE)
    +";initial catalog="+IniReadValue(CATALOG)+";user id="
    +IniReadValue(USER)+";password="+IniReadValue(PASS);
    // ReturnText="provider=SQLOLEDB;data source=172.16.36.222"+
    // ";initial catalog=RemoteEdu;user id=sa"
    // +";password=1234567890";

    return ReturnText;

    } /// <summary>
    /// 取得连接字符串
    /// </summary>
    /// <returns></returns>
    public  static string GetConnStr()
    {
    #if WEB
    return GetConnStrWeb();
    #else
    return GetConnStrDeskTop();
    #endif
    }
    /// <summary>
    /// 从配置文件中取配置
    /// </summary>
    /// <param name="Key">键</param>
    /// <returns></returns>
    public static string IniReadValue(string Key)
    {

    StringBuilder temp = new StringBuilder(255);
    int i = GetPrivateProfileString(SECTION,Key,"",temp,255,INI_FILE);
    return temp.ToString();
    } }
      

  5.   

    StreamReader sr = new StreamReader([Path], System.Text.Encoding.Default);string sz = sr.ReadLine();
    while(sz != null)

      [do what you want to deal]
      sz = sr.ReadLine();
    }
    sr.Close();