using System.Runtime.InteropServices;
using System.Text;public class IniFile { public string path; //INI文件名 [DllImport("kernel32")] private static extern long WritePrivateProfileString(string section,string key,string val,string filePath); [DllImport("kernel32")] private static extern int GetPrivateProfileString(string section,string key,string def,StringBuilder retVal,int size,string filePath);
//声明读写INI文件的API函数 public IniFile(string INIPath) { path = INIPath; }//类的构造函数,传递INI文件名 public void IniWriteValue(string Section,string Key,string Value) { WritePrivateProfileString(Section,Key,Value,this.path); }//写INI文件 public string IniReadValue(string Section,string Key) { StringBuilder temp = new StringBuilder(255); int i = GetPrivateProfileString(Section,Key,"",temp,255,this.path); return temp.ToString(); }//读取INI文件指定 }
using System.Text;public class IniFile { public string path; //INI文件名 [DllImport("kernel32")] private static extern long WritePrivateProfileString(string section,string key,string val,string filePath); [DllImport("kernel32")] private static extern int GetPrivateProfileString(string section,string key,string def,StringBuilder retVal,int size,string filePath);
//声明读写INI文件的API函数 public IniFile(string INIPath) { path = INIPath; }//类的构造函数,传递INI文件名 public void IniWriteValue(string Section,string Key,string Value) { WritePrivateProfileString(Section,Key,Value,this.path); }//写INI文件 public string IniReadValue(string Section,string Key) { StringBuilder temp = new StringBuilder(255); int i = GetPrivateProfileString(Section,Key,"",temp,255,this.path); return temp.ToString(); }//读取INI文件指定 }
IniFile test=new IniFile(@"c:\a\b.ini");
string s1=test.IniReadValue("LOC","Item1");
现在s1的值为"1,2"再s1.split(",")[1]这就是2了
FileStream myf=new FileStream("f:\\zhu.ini",FileMode.Open,FileAccess.Read);
StreamReader myreader=new StreamReader(myf); string a="";//存放结果
string s="";
string k="";
int pos=0;
s=myreader.ReadLine();
while(s!=null)
{
s=s.Trim();
if(s.Substring(0,4)=="Item")
{
pos=s.IndexOf("=");
if(k=="")
{
a=s.Substring(pos+1,s.Length-1-pos);
}
else
{
k=s.Substring(pos+1,s.Length-1-pos);
a=a+","+k;
}
}
s=myreader.ReadLine(); }
string []box1=a.Split(',');//转成数组
//show result
for(int i=0;i<box1.Length;i++)
{ this.listBox1.Items.Add(box1[i].ToString());
} //show result
for(int i=1;i<box1.Length;i=i+2)
{ this.listBox2.Items.Add(box1[i].ToString());
}