-我的论坛-
我的问题
我参与的问题
我得分的问题
我的信誉分
我的收藏夹
短信息
使用帮助
我要投诉
— 相关专题 —
API与函数
C#程序发布
C#对数据库的操作
新手提问必读
CSDN客户端工具
— 人才招聘 —
北京金山软件有限...
重庆海特科技发展...
北京飞天诚信科技...
广东长城计算机软...
北京泰思达网络通...
北京市鑫楷慧德电...
广州博汇数码科技...
北京百联网图科技...
北京用友华表软件...
北京启迪世纪通讯...
IONA科技公司亚太...
北京广联达慧中软...
北京炎黄新星网络...
清华大学(北京)
— 热销商品 —
《软件创富》
程序员杂志2001增...
程序员大本营2001...
程序员大本营2001...
Visual Studio β...
《程序员》杂志20...
《程序员》杂志20...
— 培训信息 —
中国四达独家授权...
达洋行教育招生中...
中国国际工程和材...
中商集团经济合作...
北京康飞理想科技...
首都师大出国留学...
— 图书信息 —
软件创富
CSDN
-
专家门诊
-
.NET技术 C#问题
回复
|
推荐
|
收藏
|
专题
|
公告
|
管理
|
加入FAQ
|
保存
|
回复通知
|
关闭窗口
主 题:
怎么读取文本文件?
作 者:
wushengshan (wushengshan)
等 级:
信 誉 值:
100
所属论坛:
.NET技术 C#
问题点数:
20
回复次数:
11
发表时间:
2003-8-29 13:39:16
怎么读取一个指定路径下的一个文本文件里的内容赋给一个字符串变量(string aa)呢?
回复人:
cocosoft(pengyun)
(
) 信誉:111
2003-8-29 13:43:09
得分:
0
通过流来实现。
Top
回复人:
cnhgj(戏子)
(
) 信誉:100
2003-8-29 13:45:01
得分:
0
读出内容后赋值给aa不就行了?
Top
回复人:
benzite(小禾)
(
) 信誉:100
2003-8-29 13:46:44
得分:
1
using System.IO; StreamReader sr=new StreamReader("path+fileName"); aa=sr.ReadToEnd(); sr.Close();
Top
回复人:
albert2000(albert)
(
) 信誉:100
2003-8-29 13:47:06
得分:
1
using System; using System.IO; using System.Text; class Test { public static void Main() { string path = @"c:\temp\MyTest.txt"; // Delete the file if it exists. if (!File.Exists(path)) { // Create the file. using (FileStream fs = File.Create(path)) { Byte[] info = new UTF8Encoding(true).GetBytes("This is some text in the file."); // Add some information to the file. fs.Write(info, 0, info.Length); } } // Open the stream and read it back. using (FileStream fs = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.None)) { byte[] b = new byte[1024]; UTF8Encoding temp = new UTF8Encoding(true); while (fs.Read(b,0,b.Length) > 0) { Console.WriteLine(temp.GetString(b)); } try { // Try to get another handle to the same file. using (FileStream fs2 = File.Open(path, FileMode.Open)) { // Do some task here. } } catch (Exception e) { Console.Write("Opening the file twice is disallowed."); Console.WriteLine(", as expected: {0}", e.ToString()); } } } }
Top
回复人:
superzuoluo(球星)
(
) 信誉:100
2003-8-29 13:48:07
得分:
1
StreamReader sReader = null; try { sReader = new StreamReader("c:\\1.txt",System.Text.Encoding.Default); string strText = null; while(sReader.Peek() != -1) strText += sReader.ReadLine() + "\r"; MessageBox.Show(strText); } finally { if(sReader != null) sReader.Close(); }
Top
回复人:
gujianxin(木头象)
(
) 信誉:100
2003-8-29 13:57:56
得分:
1
1,full context read 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); } } } 还要注意的就是字符集的问题,如果你的txt是ascii编码的话,就要注意了,一定要指明字符集 StreamReader sr = new StreamReader(@"c:\test1.txt",System.Text.Encoding.GetEncoding("GB2312"));
Top
回复人:
gujianxin(木头象)
(
) 信誉:100
2003-8-29 13:58:27
得分:
1
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(); } }
Top
回复人:
wushengshan(wushengshan)
(
) 信誉:100
2003-9-1 12:10:22
得分:
0
当读取中文内容时读出来的是乱码,谁能帮我看看!下面是我的代码: OpenFileDialog fileName = new OpenFileDialog(); fileName.ShowDialog(); StreamReader sr = File.OpenText(fileName.FileName); string str; //str = sr.ReadToEnd(); while((str = sr.ReadLine()) != null) { textBox1.AppendText(str); }
Top
回复人:
gujianxin(木头象)
(
) 信誉:100
2003-9-1 13:54:39
得分:
0
StreamReader sr = new StreamReader(@"c:\test1.txt",System.Text.Encoding.GetEncoding("GB2312"));
Top
回复人:
xixigongzhu(夕夕公主)
(
) 信誉:105
2003-9-1 17:26:31
得分:
1
默认的编码是UTF8。 这样试试: OpenFileDialog fileName = new OpenFileDialog(); fileName.ShowDialog(); StreamReader sr = new StreamReader(fileName.FileName, Encoding.GetEncoding("gb2312")); string str; //str = sr.ReadToEnd(); while((str = sr.ReadLine()) != null) { textBox1.AppendText(str); }
Top
回复人:
wkyjob(流星划過...)
(
) 信誉:100
2003-9-12 15:30:41
得分:
14
写: StreamWriter sw = File.CreateText(@"d:\a.txt"); sw.Write("KK:"); sw.WriteLine(textBox1.Text); sw.Close(); 读: StreamReader sr = File.OpenText(@"d:\a.txt"); string str; while((str = sr.ReadLine()) != null) { textBox2.Text = str; }
Top
网站简介
-
广告服务
-
网站地图
-
帮助信息
-
联系方式
百联美达美公司 版权所有 京ICP证020026号
Copyright ? CSDN.net, Inc. All rights reserved