?C#怎么去读取(*.txt)或(*.fig)中的内容!

yinliaobao 2009-03-28 05:50:46
列入:
我的(*.txt)或(*.fig)中的内容有:
database = master
uis = sa
pwd = aa

我在程序中怎么去读取这些对应的值哦 ???
...全文
203 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
蝶恋花雨 2011-03-23
  • 打赏
  • 举报
回复
StreamWriter sw = new StreamWriter(asFDFile, true, Encoding.GetEncoding(piEncodingFormat));
//asFDFile 文件名//piEncodingFormat 编码
stonemqy 2011-03-23
  • 打赏
  • 举报
回复
正则表达式吧,应该是比较方便的了。
有一个教程,我一点不懂,花了两三个小时看完后就能写一般的正则表达式了。可以看看
正则表达式30分钟入门教程
Hamber_Bao 2011-03-23
  • 打赏
  • 举报
回复
利用System.IO下的StreamReader读取
首先引入命名空间System.IO;

FileStream fs = new FileStream("路径",FileMode枚举类型的值(设定操作文件的方式));
StreamReader sr = new StreamReader(fs);
string content;
while((content = sr.ReadLine()) != null)
{
Console.Write(content);
}

sr.Close();
fs.Close();
636f6c696e 2011-03-22
  • 打赏
  • 举报
回复
ReadFile
然后根据正则表达式一一获取即可
你这个问题可以先按行读取,然后string.split(" = ");
然后拿偶数个string就是你想要的值
hzleexia 2011-03-22
  • 打赏
  • 举报
回复
还有忘说了...
记得引用一下using System.Runtime.InteropServices;
hzleexia 2011-03-22
  • 打赏
  • 举报
回复
[DllImport("kernel32")]
private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal,
int size, string filePath);
/// <summary>
/// 获取文件中指定节点对应值
/// </summary>
/// <param name="projectName">项目名</param>
/// <param name="key">节点名</param>
/// <param name="filePath">文件路径</param>
/// <returns></returns>
public string ReadValue(string projectName, string key, string filePath)
{
var temp = new StringBuilder(500);
int i = GetPrivateProfileString(projectName, key, "", temp, 500, filePath).ToString();
return temp.ToString();
}





然后你txt文件中内容的格式特定一下

[projectName]
key=value
key1=value
也就是
[connectionstrings]
database = master
uis = sa
pwd = aa


[DllImport("kernel32")]这是引用名为kernel32的动态类库
private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal,
int size, string filePath);
是该类库中的一个方法
Eric_zdhz 2011-03-21
  • 打赏
  • 举报
回复
按文本读出来以后,使用正则表达式分析字符串就能得到需要的值。
yalan 2011-03-21
  • 打赏
  • 举报
回复
StreamReader什么都能读
在sr.ReadLine的时候判断

或者更好的方案是:
用StreamReader将数据读入xml或者DataSet,就能使用Linq或者Sql来检索数据了
kid_wang 2011-03-21
  • 打赏
  • 举报
回复
先看看这些值前后是不是跟了什么有规律的字符或者数字,
或者,换行、空格,总之是能区分,分割它们的东西,然后用正则表达式取得。
kid_wang 2011-03-21
  • 打赏
  • 举报
回复
楼上几位大哥,人家问的是——
我在程序中怎么去读取
database = master
uis = sa
pwd = aa
这些对应的值哦 ???
不是问的怎么读这文档。OMG,答问题的时候,感情大家都不看完就答啊。晕。
hzleexia 2011-03-21
  • 打赏
  • 举报
回复
2楼正解、用StreamReader去读文档就O了...不止是txt和fig格式的...ini、XML等格式也都能读到
yagebu1983 2009-03-28
  • 打赏
  • 举报
回复
按行读取。。。
zjj1211 2009-03-28
  • 打赏
  • 举报
回复
jf
wuyq11 2009-03-28
  • 打赏
  • 举报
回复
也可通过API函数GetPrivateProfileString等实现操作
http://www.cnblogs.com/tianxiao/articles/1209300.html
http://www.cnblogs.com/inuli/archive/2008/12/29/1364542.html
yangqidong 2009-03-28
  • 打赏
  • 举报
回复
如果还要取出等号后面的值。这样:
StreamReader reader = new StreamReader("xx.txt");
string data;
while((data=reader.Read())!=null)
{
Console.WriteLine(data.Split('=')[1]);//Split按等号拆分成一个字符串数组
}
yangqidong 2009-03-28
  • 打赏
  • 举报
回复
StreamReader reader = new StreamReader("xx.txt");
string data;
while((data=reader.Read())!=null)
{
Console.WriteLine(data);//这样可以输出文件每一行
}

111,126

社区成员

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

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

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