C#读取本地ini配置文件内容至TextBox

ini配置文件内容:*********************************************************[my]
email=etroray@vip.qq.com
password=1287///.
empire=1
clear=1
clearxq=523700000
sn=4a73-a642-efdb-660b-abe7-7b57
total=2031066300
totalmoney=1825764800
totalempre=205301500
totalitem=16*********************************************************文件里的这两句代码email=etroray@vip.qq.com
password=*****我想利用C#写一个程序Text1中的显示读取的emai “="后面的内容Text2种的显示读取的password “="后面的内容请问这样的程序怎样写?

第1个回答  2013-06-04
读需要用到API:[DllImport("kernel32")]
private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);这里先写到一个方法里去:public static string inipath;//这个就是Ini文件存放的路径public string IniReadString(string Section, string Key)
{
StringBuilder temp = new StringBuilder(500);
int i = GetPrivateProfileString(Section, Key, "", temp, 500,inipath);
return temp.ToString();
}调用的方法:Text1.textp=IniReadString("my","email");Text2.textp=IniReadString("my","password ");就这么简单。
第2个回答  2013-06-04
我刚写了个读取这种配置文件的通用类思路大概是这样的,首先你可以找到email这个字符串的位置,然后定位到email后面最近的一个‘=’号和最近的换行字符,理论上是\r\n 然后截取这段字符就可以了获取password后的方法一致你可以尝试下获取指定字符串的位置可以使用:string.IndexOf或者使用正则表达式来作业可以当然,这是在配置文件中不存在相同项的前提下使用。尽量使用如email=来定位。。。。。
相似回答