如何删除.ini文件中的制定section及其中的全部内容?
如:
.ini为:[列表1]
nameCount=2
name1=2
name2=赛段
[列表2]
SongCount=3
name1=d
name2=d
name3=re我想删除列表2及其全部内容,怎么办?
如:
.ini为:[列表1]
nameCount=2
name1=2
name2=赛段
[列表2]
SongCount=3
name1=d
name2=d
name3=re我想删除列表2及其全部内容,怎么办?
调试欢乐多
//查找文件是否存在
if (!File.Exists(strFile))
return; //流文件
StreamReader sr = new StreamReader(strFile);
try
{
String line;
// Read and display lines from the file until the end of
// the file is reached.
while ((line = sr.ReadLine()) != null)
{
//判断配置类型,并赋值:
//服务器
if(line.IndexOf("data source=") != -1)
this.FWQ = line.Substring(line.IndexOf("=")+1); }
catch (Exception e)
{
// Let the user know what went wrong.
MessageBox.Show("The file could not be read:"+e.Message);
}写文件://获取当前文件路径
string path = Directory.GetCurrentDirectory();
string strFile = path + @"\conf.ini"; //流文件
StreamWriter sr = File.CreateText(strFile);
try
{
sr.WriteLine ("data source=" + this.FWQ);
sr.WriteLine ("Connect Timeout=30");
sr.Close();
}
catch (Exception e)
{
// Let the user know what went wrong.
MessageBox.Show("The file could not be writed:"+e.Message);
}
WritePrivateProfileString(string lpAppName, string lpKeyName, string lpString, string lpFileName)
这个API就可以做到,给大家分享一下,第二个参数设为null就可以将lpAppName这个SESSION全部删除。
建议大家以后用.ini还不如用xml,要方便多了,现在就改用xml了。