?C#怎么去读取(*.txt)或(*.fig)中的内容! 列入: 我的(*.txt)或(*.fig)中的内容有: database = master uis = sa pwd = aa 我在程序中怎么去读取这些对应的值哦 ??? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 StreamReader reader = new StreamReader("xx.txt");string data;while((data=reader.Read())!=null){ Console.WriteLine(data);//这样可以输出文件每一行} 如果还要取出等号后面的值。这样:StreamReader reader = new StreamReader("xx.txt"); string data; while((data=reader.Read())!=null) { Console.WriteLine(data.Split('=')[1]);//Split按等号拆分成一个字符串数组 } 也可通过API函数GetPrivateProfileString等实现操作http://www.cnblogs.com/tianxiao/articles/1209300.htmlhttp://www.cnblogs.com/inuli/archive/2008/12/29/1364542.html 2楼正解、用StreamReader去读文档就O了...不止是txt和fig格式的...ini、XML等格式也都能读到 楼上几位大哥,人家问的是—— 我在程序中怎么去读取 database = master uis = sa pwd = aa这些对应的值哦 ???不是问的怎么读这文档。OMG,答问题的时候,感情大家都不看完就答啊。晕。 StreamReader什么都能读在sr.ReadLine的时候判断或者更好的方案是:用StreamReader将数据读入xml或者DataSet,就能使用Linq或者Sql来检索数据了 [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=valuekey1=value也就是[connectionstrings]database = masteruis = sapwd = aa[DllImport("kernel32")]这是引用名为kernel32的动态类库private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);是该类库中的一个方法 还有忘说了...记得引用一下using System.Runtime.InteropServices; ReadFile然后根据正则表达式一一获取即可你这个问题可以先按行读取,然后string.split(" = ");然后拿偶数个string就是你想要的值 利用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(); StreamWriter sw = new StreamWriter(asFDFile, true, Encoding.GetEncoding(piEncodingFormat));//asFDFile 文件名//piEncodingFormat 编码 C# 针式打印机 线程循环 C#中使用指针 高分求助。C# WinForm上的焦点问题 能不能把一个DataSet数据集中的全部数据一次性插入数据库? 大家帮忙,关于使用 DllImport 调用一个 dll 希望帮忙 询问关于泛型约束struct的问题。 急!抽奖程序。 在DataGrid焦点问题 多线程里需要做手动GC回收吗?如果需要做,应该怎么处理? 将 varchar 转换为数据类型 numeric 时出现算术溢出错误。
string data;
while((data=reader.Read())!=null)
{
Console.WriteLine(data);//这样可以输出文件每一行
}
StreamReader reader = new StreamReader("xx.txt");
string data;
while((data=reader.Read())!=null)
{
Console.WriteLine(data.Split('=')[1]);//Split按等号拆分成一个字符串数组
}
http://www.cnblogs.com/tianxiao/articles/1209300.html
http://www.cnblogs.com/inuli/archive/2008/12/29/1364542.html
我在程序中怎么去读取
database = master
uis = sa
pwd = aa
这些对应的值哦 ???
不是问的怎么读这文档。OMG,答问题的时候,感情大家都不看完就答啊。晕。
在sr.ReadLine的时候判断或者更好的方案是:
用StreamReader将数据读入xml或者DataSet,就能使用Linq或者Sql来检索数据了
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);
是该类库中的一个方法
记得引用一下using System.Runtime.InteropServices;
然后根据正则表达式一一获取即可
你这个问题可以先按行读取,然后string.split(" = ");
然后拿偶数个string就是你想要的值
首先引入命名空间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();
//asFDFile 文件名//piEncodingFormat 编码