C#在winform中读写ini文件
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文件的更多相关文章
- Windows中读写ini文件
.ini 文件是Initialization File的缩写,即初始化文件,是windows的系统配置文件所采用的存储格式,来配置应用软件以实现不同用户的要求.配置文件有很多种如ini配置文件,XML ...
- C#中读写INI文件
C#中读写INI文件 c#的类没有直接提供对ini文件的操作支持,可以自己包装win api的WritePrivateProfileString和GetPrivateProfileString函数实现 ...
- 如何在C#中读写INI文件
INI文件就是扩展名为"ini"的文件.在Windows系统中,INI文件是很多,最重要的就是"System.ini"."System32.ini&q ...
- C#中读写INI文件
INI文件就是扩展名为“ini”的文件.在Windows系统中,INI文件是很多,最重要的就是“System.ini”.“System32.ini”和“Win.ini”.该文件主要存放用户所做的选择以 ...
- c# 利用动态库DllImport("kernel32")读写ini文件(提供Dmo下载)
c# 利用动态库DllImport("kernel32")读写ini文件 自从读了设计模式,真的会改变一个程序员的习惯.我觉得嘛,经验也可以从一个人的习惯看得出来,看他的代码编写习 ...
- VB读写INI文件的四个函数以及相关API详细说明
WritePrivateProfileString函数说明 来源:http://blog.csdn.net/wjb9921/article/details/2005000 在我们写的程序当中,总有一 ...
- C# 读写INI 文件
INI 格式: [Section1] KeyWord1 = Value1 KeyWord2 = Value2 ... [Section2] KeyWord3 = Value3 KeyWord4 = V ...
- WIN32读写INI文件方法
在程序中经常要用到设置或者其他少量数据的存盘,以便程序在下一次执行的时候可以使用,比如说保存本次程序执行时窗口的位置.大小.一些用户设置的 数据等等,在 Dos 下编程的时候,我们一般自己产生一个 ...
- 读写ini文件
C# 使用文件流来读写ini文件 背景 之前采用ini文件作为程序的配置文件,觉得这种结构简单明了,配置起来也挺方便.然后操作方式是通过WindowsAPI,然后再网上找到一个基于WindowsAPI ...
随机推荐
- 2016广东工业大学新生杯决赛网络同步赛暨全国新生邀请赛 题解&源码
Problem A: pigofzhou的巧克力棒 Description 众所周知,pigofzhou有许多妹子.有一天,pigofzhou得到了一根巧克力棒,他想把这根巧克力棒分给他的妹子们.具体 ...
- Codeforces Round #356 (Div. 1) C. Bear and Square Grid
C. Bear and Square Grid time limit per test 3 seconds memory limit per test 256 megabytes input stan ...
- HDU 1012 u Calculate e【暴力打表,水】
u Calculate e Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- Codeforces 626C Block Towers(二分)
C. Block Towers time limit per test:2 seconds memory limit per test:256 megabytes input:standard inp ...
- CodeForces-2015 HIAST Collegiate Programming Contest-Gym-100952A-Who is the winner?
A. Who is the winner? time limit per test 1 second memory limit per test 64 megabytes input standard ...
- Codeforces 626C
...
- 在.Net中将RocketMQ跑起来_入门篇【2】
上一篇讲了如何再控制台将RocketMQ跑起来,本篇讲解,在asp.net mvc种跑起来,含(发布.订阅). 本次将不挨个贴源码,直接展示目录,根据上一篇文章,进行相应的调整即可. 1.新建一个类库 ...
- docker运行dubbo-admin
一:简介 dubbo-admin是dubbo框架的管理平台. 二: 创建继续镜像 Dockerfile FROM fangjipu/jdk8:8 RUN yum -y install epel-rel ...
- mysql按照天统计报表,当天没有数据,填0
1.问题复现: 按照天数统计每天的总数,如果其中有几天没有数据,那么group by 返回会忽略那几天,如何填充0?如下图,统计的10-3~10-10 7天的数据,其中只有8号和10号有数据,这样返回 ...
- javascript之fill()方法
无意中看到fill这个方法,有些不解,起初以为是人家自定义的方法,后来才发觉原来不是,javascript里面是真的有这个方法,于是特地学习了下. fill()方法的作用是使用一个固定值来替换数组中的 ...