如何删除.ini文件中的制定section及其中的全部内容?

萬里無雲 2005-08-26 03:12:46
如何删除.ini文件中的制定section及其中的全部内容?
如:
.ini为:

[列表1]
nameCount=2
name1=2
name2=赛段
[列表2]
SongCount=3
name1=d
name2=d
name3=re

我想删除列表2及其全部内容,怎么办?
...全文
614 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
萬里無雲 2005-09-02
  • 打赏
  • 举报
回复
已经解决!
WritePrivateProfileString(string lpAppName, string lpKeyName, string lpString, string lpFileName)
这个API就可以做到,给大家分享一下,第二个参数设为null就可以将lpAppName这个SESSION全部删除。
建议大家以后用.ini还不如用xml,要方便多了,现在就改用xml了。
liuq 2005-08-28
  • 打赏
  • 举报
回复
用api好像只有添加,删除key也只是把值删掉,key名还留着。网上有用c#写的ini解析的类,算法写得挺复杂,我没仔细看。我自己做了一个,读写还用api,用c#中文件处理和字符串处理枚举出所有section和key,放到列表框里。删除的时候也是用c#删除对应行就可以了,就是字符串比较一下。
mmens 2005-08-26
  • 打赏
  • 举报
回复
读文件:
//查找文件是否存在
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);
}
TechEye 2005-08-26
  • 打赏
  • 举报
回复
用 TextReader 读到内存再把须要的写回去.

或API有一些操作ini profile 的函数

111,092

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧