class WY_INI
{
static string IniFileName;
static char[] TrimChar = { ' ', '\t' };
public string[] GetSects()
{
string[] Sects = null; if (File.Exists(IniFileName))
{
string str;
ArrayList ls = new ArrayList();
TextReader tr = File.OpenText(IniFileName);
while ((str = tr.ReadLine()) != null)
{
str = str.Trim();
if ((str.StartsWith("[")) && (str.EndsWith("]")))
ls.Add(str);
}
tr.Close();
if (ls.Count > )
{
Sects = new string[ls.Count];
for (int i = ; i < ls.Count; i++)
{
Sects[i] = ls[i].ToString();
}
}
}
return Sects;
}
public static void PutINI(string sect, string keystr, string valuestr, string IniFileName)
{
ArrayList ls = new ArrayList();
bool SectOK = false;
bool SetOK = false;
if (File.Exists(IniFileName))
{
int pos1;
string substr;
string str;
TextReader tr = File.OpenText(IniFileName);
while ((str = tr.ReadLine()) != null)
{
ls.Add(str);
}
tr.Close();
//开始寻找关键字,如果找不到,则在这段的最后一行插入,然后再整体的保存一下INI文件。
for (int i = ; i < ls.Count; i++)
{
str = ls[i].ToString();
if (str.StartsWith("[") && SectOK) //先判断是否到下一段中了,如果本来就是最后一段,那就有可能永远也不会发生了。
{
SetOK = true; //如果在这一段中没有找到,并且已经要进入下一段了,就直接在这一段末添加了。
ls.Insert(i, keystr.Trim() + "=" + valuestr);
break;//如果到下一段了,则直接退出就好。
}
if (SectOK)
{
pos1 = str.IndexOf("=");
if (pos1 > )
{
substr = str.Substring(, pos1);
substr.Trim(TrimChar);
//如果在这一段中找到KEY了,直接修改就好了。
if (substr.Equals(keystr, StringComparison.OrdinalIgnoreCase) && SectOK) //是在此段中,并且KEYSTR前段也能匹配上。
{
SetOK = true;
ls[i] = keystr.Trim() + "=" + valuestr;
break;
}
}
}
if (str.StartsWith("[" + sect + "]")) //判断是否到需要的段中了。
SectOK = true;
}
if (SetOK == false)
{
SetOK = true;
if (!SectOK) //如果没有找到段,则需要再添加段。
{
ls.Add("[" + sect + "]");
}
ls.Add(keystr.Trim() + "=" + valuestr);
}
} //如果文件不存在,则需要建立文件。
else
{
ls.Clear();
ls.Add("##文件创建:" + DateTime.Now.ToString() + "##");
ls.Add("[" + sect + "]");
ls.Add(keystr.Trim() + "=" + valuestr);
}
//if (File.Exists(IniFileName)) //删除源文件。
//{
// File.Delete(IniFileName);
//}
TextWriter tw = File.CreateText(IniFileName);
//string[] strList = new string[ls.Count];
for (int i = ; i < ls.Count; i++)
{
//strList[i] = ls[i].ToString();
tw.WriteLine(ls[i].ToString());
}
tw.Flush();
tw.Close();
//File.WriteAllLines(IniFileName, strList);
}
public static string GetINI(string sect, string keystr, string defaultstr, string IniFileName)
{
string retstr = defaultstr;
if (File.Exists(IniFileName))
{
bool SectOK = false;
int pos1;
string substr;
string str;
ArrayList ls = new ArrayList();
TextReader tr = File.OpenText(IniFileName);
while ((str = tr.ReadLine()) != null)
{
str = str.Trim();
if (str.StartsWith("[") && SectOK) //先判断是否到下一段中了。
{
break;//如果到下一段了,则直接退出就好。
}
if (SectOK)
{
pos1 = str.IndexOf("=");
if (pos1 > )
{
substr = str.Substring(, pos1);
substr.Trim(TrimChar);
if (substr.Equals(keystr, StringComparison.OrdinalIgnoreCase)) //是在此段中,并且KEYSTR前段也能匹配上。
{
retstr = str.Substring(pos1 + ).Trim(TrimChar);
break;
}
}
}
if (str.StartsWith("[" + sect + "]")) //判断是否到需要的段中了。
SectOK = true;
}
tr.Close();
}
return retstr;
}
//读整数
public static int GetINI(string Section, string Ident, int Default,string IniFileName)
{
string intStr = GetINI(Section, Ident, Convert.ToString(Default),IniFileName);
try
{
return Convert.ToInt32(intStr);
}
catch
{
return Default;
}
} //写整数
public static void PutINI(string Section, string Ident, int Value, string IniFileName)
{
PutINI(Section, Ident, Value.ToString(),IniFileName);
} //读布尔
public static bool ReadBool(string Section, string Ident, bool Default, string IniFileName)
{
try
{
return Convert.ToBoolean(GetINI(Section, Ident, Convert.ToString(Default),IniFileName));
}
catch
{
return Default;
}
}
//写Bool
public static void PutINI(string Section, string Ident, bool Value,string IniFileName)
{
PutINI(Section, Ident, Convert.ToString(Value),IniFileName);
} /////////////////////////////////////////////////////////////////////////
//使用此INI文件的特例(自己使用)
public string GetParam(string KeyStr, string Default,string IniFileName)
{
string str;
str = GetINI("Params", KeyStr, "???", IniFileName);
if (str == "???")
{
PutINI("Params", KeyStr, Default, IniFileName);
str = Default;
}
return str;
}
public void UpdateParam(string KeyStr, string ValueStr,string IniFileName)
{
PutINI("Params", KeyStr, ValueStr, IniFileName);
}
}

路径必须用全路径

       static void Main(string[] args)
{ string path = System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase) + "\\config5.ini";
Console.WriteLine("获取当前路径:" + testPath2);
int temp;
int temp2;
Random rand = new Random();
Random rand2 = new Random();
string s;
while (true)
{
temp2 = rand.Next(, );
temp = rand.Next(, );
ZT_INI1.PutINI("system" + temp2, "print" + temp, "随机数" + temp, path);
s=ZT_INI1.GetINI("system" + temp2, "print" + temp, "随机数" + temp, path);
Console.WriteLine(s); }
}

改良过了,不会出现空行和丢失问题,测试的时候写了个循环跑了一会儿~~~~

C#在winform中读写ini文件的更多相关文章

  1. Windows中读写ini文件

    .ini 文件是Initialization File的缩写,即初始化文件,是windows的系统配置文件所采用的存储格式,来配置应用软件以实现不同用户的要求.配置文件有很多种如ini配置文件,XML ...

  2. C#中读写INI文件

    C#中读写INI文件 c#的类没有直接提供对ini文件的操作支持,可以自己包装win api的WritePrivateProfileString和GetPrivateProfileString函数实现 ...

  3. 如何在C#中读写INI文件

    INI文件就是扩展名为"ini"的文件.在Windows系统中,INI文件是很多,最重要的就是"System.ini"."System32.ini&q ...

  4. C#中读写INI文件

    INI文件就是扩展名为“ini”的文件.在Windows系统中,INI文件是很多,最重要的就是“System.ini”.“System32.ini”和“Win.ini”.该文件主要存放用户所做的选择以 ...

  5. c# 利用动态库DllImport("kernel32")读写ini文件(提供Dmo下载)

    c# 利用动态库DllImport("kernel32")读写ini文件 自从读了设计模式,真的会改变一个程序员的习惯.我觉得嘛,经验也可以从一个人的习惯看得出来,看他的代码编写习 ...

  6. VB读写INI文件的四个函数以及相关API详细说明

    WritePrivateProfileString函数说明  来源:http://blog.csdn.net/wjb9921/article/details/2005000 在我们写的程序当中,总有一 ...

  7. C# 读写INI 文件

    INI 格式: [Section1] KeyWord1 = Value1 KeyWord2 = Value2 ... [Section2] KeyWord3 = Value3 KeyWord4 = V ...

  8. WIN32读写INI文件方法

      在程序中经常要用到设置或者其他少量数据的存盘,以便程序在下一次执行的时候可以使用,比如说保存本次程序执行时窗口的位置.大小.一些用户设置的 数据等等,在 Dos 下编程的时候,我们一般自己产生一个 ...

  9. 读写ini文件

    C# 使用文件流来读写ini文件 背景 之前采用ini文件作为程序的配置文件,觉得这种结构简单明了,配置起来也挺方便.然后操作方式是通过WindowsAPI,然后再网上找到一个基于WindowsAPI ...

随机推荐

  1. ThinkPHP3.2中英文切换!

    小伙伴们好久不见!!!   最近公司项目版本升级,小梦已经忙成了狗,无暇顾及文章,今天抽时间写一篇助助兴!   用Thinkphp这个国产框架已经2年多了,现在有一个小功能:网站中英文切换功能,当然这 ...

  2. Django之cookie验证

    先不用太多的蚊子描述什么是cookie,先做一个小实验: 此时我们在谷歌浏览器(一个客户端)和IE浏览器(另一个用户)测试: 刺客我们发现在两台浏览器都可以访问,而且不用进入login验证就可以登录, ...

  3. 洛谷 P1914 小书童——密码【字符串+模拟】

    P1914 小书童——密码 题目背景 某蒟蒻迷上了“小书童”,有一天登陆时忘记密码了(他没绑定邮箱or手机),于是便把问题抛给了神犇你. 题目描述 蒟蒻虽然忘记密码,但他还记得密码是由一串字母组成.且 ...

  4. Ping pong(树状数组求序列中比某个位置上的数小的数字个数)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2492 Ping pong Time Limit: 2000/1000 MS (Java/Others) ...

  5. HDU_4826

    Labyrinth Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  6. PL/SQL 一个数据对象一个事务(rollback,submit)

    /*********************************************** 一个数据对象一个事务(且记录错误信息到处理对象) ************************** ...

  7. JavaScript实现职责链模式

    什么是职责链模式 职责链模式的定义是:使多个对象都有机会处理请求,从而避免请求的发送者和接收者之间的耦合关系,将这些对象连成一条链,并沿着这条链传递该请求,直到有一个对象处理它为止.举个例子:当你从公 ...

  8. DEDECMS点击主栏目默认显示第一个子栏目列表的方法

    本文实例讲述了DEDECMS点击主栏目默认显示第一个子栏目列表的方法.分享给大家供大家参考.具体分析如下: 今天公司有个需求是,点击导航上的父栏目进去默认显示第一个子栏目的列表,以下是具体实现方法,可 ...

  9. Python3 引入模块的方法

    例子 import random 产生随机整数 import random secret = random.randint(0,10)

  10. 【编程技巧】alert vs Ext.Msg.alert

    alert会阻塞程序的运行. Ext.Msg.alert是异步的,它的调用并不会停止浏览器中代码的执行.