如何删除.ini文件中的制定section及其中的全部内容?
如:
.ini为:[列表1]
nameCount=2
name1=2
name2=赛段
[列表2]
SongCount=3
name1=d
name2=d
name3=re我想删除列表2及其全部内容,怎么办?

解决方案 »

  1.   

    用 TextReader 读到内存再把须要的写回去.或API有一些操作ini profile 的函数
      

  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);
    }
      

  3.   

    用api好像只有添加,删除key也只是把值删掉,key名还留着。网上有用c#写的ini解析的类,算法写得挺复杂,我没仔细看。我自己做了一个,读写还用api,用c#中文件处理和字符串处理枚举出所有section和key,放到列表框里。删除的时候也是用c#删除对应行就可以了,就是字符串比较一下。
      

  4.   

    已经解决!
    WritePrivateProfileString(string lpAppName, string lpKeyName, string lpString, string lpFileName)
    这个API就可以做到,给大家分享一下,第二个参数设为null就可以将lpAppName这个SESSION全部删除。
    建议大家以后用.ini还不如用xml,要方便多了,现在就改用xml了。