怎样从.txt中读取字符串?

woliuliudada 2003-06-24 09:13:24
我在帮助中没找着例子:(
请给个例子,谢啦:)
...全文
133 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
brightheroes 2003-06-25
  • 打赏
  • 举报
回复
木头象厉害
ai0ai 2003-06-25
  • 打赏
  • 举报
回复
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();
gujianxin 2003-06-25
  • 打赏
  • 举报
回复
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();
}

}
duying 2003-06-25
  • 打赏
  • 举报
回复
在msdn中查找System.IO,你会有惊喜发现 :)
甴曱 2003-06-24
  • 打赏
  • 举报
回复
using System.IO;//必须
StreamReader Sr=File.OpenText([文件名]);
string s=Sr.ReadLine();
tjq_tang 2003-06-24
  • 打赏
  • 举报
回复
using System.IO;
..............................
StreamReader sr = File.OpenText(@"c:\test.txt");
string s = sr.ReadLine();
//hope it helps
TheAres 2003-06-24
  • 打赏
  • 举报
回复
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);
}
}
}

110,537

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧