C#读写INI文件

摘要:
虽然微软早已经建议在WINDOWS中用注册表代替INI文件,但是在实际应用中,INI文件仍然有用武之地,尤其现在绿色软件的流行,越来越多的程序将自己的一些配置信息保存到了INI文件中。usingSystem;usingSystem.IO;usingSystem.Runtime.InteropServices;usingSystem.Text;usingSystem.Collections;usingSystem.Collections.Specialized;namespacewuyisky{  /**//**/  /**////  ///IniFiles的类  ///publicclassIniFiles  {    publicstringFileName;//INI文件名    //声明读写INI文件的API函数[DllImport]    privatestaticexternboolWritePrivateProfileString;    [DllImport]    privatestaticexternintGetPrivateProfileString;    //类的构造函数,传递INI文件名publicIniFiles    {      //判断文件是否存在FileInfofileInfo=newFileInfo;      //Todo:搞清枚举的用法if((!

虽然微软早已经建议在WINDOWS中用注册表代替INI文件,但是在实际应用中,INI文件仍然有用武之地,尤其现在绿色软件的流行,越来越多的程序将自己的一些配置信息保存到了INI文件中。

INI文件是文本文件,由若干节(section)组成,在每个带括号的标题下面,是若干个关键词(key)及其对应的值(Value)

[Section]

Key=Value

VC中提供了API函数进行INI文件的读写操作,但是微软推出的C#编程语言中却没有相应的方法,下面是一个C#ini文件读写类,从网上收集的,很全,就是没有对section的改名功能,高手可以增加一个。

C#读写INI文件第1张usingSystem;
C#读写INI文件第1张
usingSystem.IO;
C#读写INI文件第1张
usingSystem.Runtime.InteropServices;
C#读写INI文件第1张
usingSystem.Text;
C#读写INI文件第1张
usingSystem.Collections;
C#读写INI文件第1张
usingSystem.Collections.Specialized;
C#读写INI文件第1张
C#读写INI文件第8张C#读写INI文件第9张
namespacewuyiskyC#读写INI文件第10张{
C#读写INI文件第11张C#读写INI文件第12张  
/**//**/
C#读写INI文件第11张C#读写INI文件第12张  
/**////<summary>
C#读写INI文件第15张  
///IniFiles的类
C#读写INI文件第16张  
///</summary>

C#读写INI文件第15张publicclassIniFiles
C#读写INI文件第11张C#读写INI文件第12张  
C#读写INI文件第10张{
C#读写INI文件第15张    
publicstringFileName;//INI文件名
C#读写INI文件第15张    
//声明读写INI文件的API函数
C#读写INI文件第15张
[DllImport("kernel32")]
C#读写INI文件第15张    
privatestaticexternboolWritePrivateProfileString(stringsection,stringkey,stringval,stringfilePath);
C#读写INI文件第15张    [DllImport(
"kernel32")]
C#读写INI文件第15张    
privatestaticexternintGetPrivateProfileString(stringsection,stringkey,stringdef,byte[]retVal,intsize,stringfilePath);
C#读写INI文件第15张    
//类的构造函数,传递INI文件名
C#读写INI文件第15张
publicIniFiles(stringAFileName)
C#读写INI文件第11张C#读写INI文件第12张    
C#读写INI文件第10张{
C#读写INI文件第15张      
//判断文件是否存在
C#读写INI文件第15张
FileInfofileInfo=newFileInfo(AFileName);
C#读写INI文件第15张      
//Todo:搞清枚举的用法
C#读写INI文件第15张
if((!fileInfo.Exists))
C#读写INI文件第11张C#读写INI文件第12张      
C#读写INI文件第10张{//||(FileAttributes.DirectoryinfileInfo.Attributes))
C#读写INI文件第15张        
//文件不存在,建立文件
C#读写INI文件第15张
System.IO.StreamWritersw=newSystem.IO.StreamWriter(AFileName,false,System.Text.Encoding.Default);
C#读写INI文件第15张        
try
C#读写INI文件第11张C#读写INI文件第12张        
C#读写INI文件第10张{
C#读写INI文件第15张          sw.Write(
"#表格配置档案");
C#读写INI文件第15张          sw.Close();
C#读写INI文件第16张        }

C#读写INI文件第15张
C#读写INI文件第15张        
catch
C#读写INI文件第11张C#读写INI文件第12张        
C#读写INI文件第10张{
C#读写INI文件第15张          
throw(newApplicationException("Ini文件不存在"));
C#读写INI文件第16张        }

C#读写INI文件第16张      }

C#读写INI文件第15张      
//必须是完全路径,不能是相对路径
C#读写INI文件第15张
FileName=fileInfo.FullName;
C#读写INI文件第16张    }

C#读写INI文件第15张    
//写INI文件
C#读写INI文件第15张
publicvoidWriteString(stringSection,stringIdent,stringValue)
C#读写INI文件第11张C#读写INI文件第12张    
C#读写INI文件第10张{
C#读写INI文件第15张      
if(!WritePrivateProfileString(Section,Ident,Value,FileName))
C#读写INI文件第11张C#读写INI文件第12张      
C#读写INI文件第10张{
C#读写INI文件第15张 
C#读写INI文件第15张        
throw(newApplicationException("写Ini文件出错"));
C#读写INI文件第16张      }

C#读写INI文件第16张    }

C#读写INI文件第15张    
//读取INI文件指定
C#读写INI文件第15张
publicstringReadString(stringSection,stringIdent,stringDefault)
C#读写INI文件第11张C#读写INI文件第12张    
C#读写INI文件第10张{
C#读写INI文件第15张      Byte[]Buffer
=newByte[65535];
C#读写INI文件第15张      
intbufLen=GetPrivateProfileString(Section,Ident,Default,Buffer,Buffer.GetUpperBound(0),FileName);
C#读写INI文件第15张      
//必须设定0(系统默认的代码页)的编码方式,否则无法支持中文
C#读写INI文件第15张
strings=Encoding.GetEncoding(0).GetString(Buffer);
C#读写INI文件第15张      s
=s.Substring(0,bufLen);
C#读写INI文件第15张      
returns.Trim();
C#读写INI文件第16张    }

C#读写INI文件第15张
C#读写INI文件第15张    
//读整数
C#读写INI文件第15张
publicintReadInteger(stringSection,stringIdent,intDefault)
C#读写INI文件第11张C#读写INI文件第12张    
C#读写INI文件第10张{
C#读写INI文件第15张      
stringintStr=ReadString(Section,Ident,Convert.ToString(Default));
C#读写INI文件第15张      
try
C#读写INI文件第11张C#读写INI文件第12张      
C#读写INI文件第10张{
C#读写INI文件第15张        
returnConvert.ToInt32(intStr);
C#读写INI文件第15张
C#读写INI文件第16张      }

C#读写INI文件第15张      
catch(Exceptionex)
C#读写INI文件第11张C#读写INI文件第12张      
C#读写INI文件第10张{
C#读写INI文件第15张        Console.WriteLine(ex.Message);
C#读写INI文件第15张        
returnDefault;
C#读写INI文件第16张      }

C#读写INI文件第16张    }

C#读写INI文件第15张
C#读写INI文件第15张    
//写整数
C#读写INI文件第15张
publicvoidWriteInteger(stringSection,stringIdent,intValue)
C#读写INI文件第11张C#读写INI文件第12张    
C#读写INI文件第10张{
C#读写INI文件第15张      WriteString(Section,Ident,Value.ToString());
C#读写INI文件第16张    }

C#读写INI文件第15张
C#读写INI文件第15张    
//读布尔
C#读写INI文件第15张
publicboolReadBool(stringSection,stringIdent,boolDefault)
C#读写INI文件第11张C#读写INI文件第12张    
C#读写INI文件第10张{
C#读写INI文件第15张      
try
C#读写INI文件第11张C#读写INI文件第12张      
C#读写INI文件第10张{
C#读写INI文件第15张        
returnConvert.ToBoolean(ReadString(Section,Ident,Convert.ToString(Default)));
C#读写INI文件第16张      }

C#读写INI文件第15张      
catch(Exceptionex)
C#读写INI文件第11张C#读写INI文件第12张      
C#读写INI文件第10张{
C#读写INI文件第15张        Console.WriteLine(ex.Message);
C#读写INI文件第15张        
returnDefault;
C#读写INI文件第16张      }

C#读写INI文件第16张    }

C#读写INI文件第15张
C#读写INI文件第15张    
//写Bool
C#读写INI文件第15张
publicvoidWriteBool(stringSection,stringIdent,boolValue)
C#读写INI文件第11张C#读写INI文件第12张    
C#读写INI文件第10张{
C#读写INI文件第15张      WriteString(Section,Ident,Convert.ToString(Value));
C#读写INI文件第16张    }

C#读写INI文件第15张
C#读写INI文件第15张    
//从Ini文件中,将指定的Section名称中的所有Ident添加到列表中
C#读写INI文件第15张
publicvoidReadSection(stringSection,StringCollectionIdents)
C#读写INI文件第11张C#读写INI文件第12张    
C#读写INI文件第10张{
C#读写INI文件第15张      Byte[]Buffer
=newByte[16384];
C#读写INI文件第15张      
//Idents.Clear();
C#读写INI文件第15张

C#读写INI文件第15张      
intbufLen=GetPrivateProfileString(Section,null,null,Buffer,Buffer.GetUpperBound(0),
C#读写INI文件第15张       FileName);
C#读写INI文件第15张      
//对Section进行解析
C#读写INI文件第15张
GetStringsFromBuffer(Buffer,bufLen,Idents);
C#读写INI文件第16张    }

C#读写INI文件第15张
C#读写INI文件第15张    
privatevoidGetStringsFromBuffer(Byte[]Buffer,intbufLen,StringCollectionStrings)
C#读写INI文件第11张C#读写INI文件第12张    
C#读写INI文件第10张{
C#读写INI文件第15张      Strings.Clear();
C#读写INI文件第15张      
if(bufLen!=0)
C#读写INI文件第11张C#读写INI文件第12张      
C#读写INI文件第10张{
C#读写INI文件第15张        
intstart=0;
C#读写INI文件第15张        
for(inti=0;i<bufLen;i++)
C#读写INI文件第11张C#读写INI文件第12张        
C#读写INI文件第10张{
C#读写INI文件第15张          
if((Buffer[i]==0)&&((i-start)>0))
C#读写INI文件第11张C#读写INI文件第12张          
C#读写INI文件第10张{
C#读写INI文件第15张            Strings
=Encoding.GetEncoding(0).GetString(Buffer,start,i-start);
C#读写INI文件第15张            Strings.Add(s);
C#读写INI文件第15张            start
=i+1;
C#读写INI文件第16张          }

C#读写INI文件第16张        }

C#读写INI文件第16张      }

C#读写INI文件第16张    }

C#读写INI文件第15张    
//从Ini文件中,读取所有的Sections的名称
C#读写INI文件第15张
publicvoidReadSections(StringCollectionSectionList)
C#读写INI文件第11张C#读写INI文件第12张    
C#读写INI文件第10张{
C#读写INI文件第15张      
//Note:必须得用Bytes来实现,StringBuilder只能取到第一个Section
C#读写INI文件第15张
byte[]Buffer=newbyte[65535];
C#读写INI文件第15张      
intbufLen=0;
C#读写INI文件第15张      bufLen
=GetPrivateProfileString(null,null,null,Buffer,
C#读写INI文件第15张       Buffer.GetUpperBound(
0),FileName);
C#读写INI文件第15张      GetStringsFromBuffer(Buffer,bufLen,SectionList);
C#读写INI文件第16张    }

C#读写INI文件第15张    
//读取指定的Section的所有Value到列表中
C#读写INI文件第15张
publicvoidReadSectionValues(stringSection,NameValueCollectionValues)
C#读写INI文件第11张C#读写INI文件第12张    
C#读写INI文件第10张{
C#读写INI文件第15张      StringCollectionKeyList
=newStringCollection();
C#读写INI文件第15张      ReadSection(Section,KeyList);
C#读写INI文件第15张      Values.Clear();
C#读写INI文件第15张      
foreach(stringkeyinKeyList)
C#读写INI文件第11张C#读写INI文件第12张      
C#读写INI文件第10张{
C#读写INI文件第15张        Values.Add(key,ReadString(Section,key,
""));
C#读写INI文件第15张  
C#读写INI文件第16张      }

C#读写INI文件第16张    }

C#读写INI文件第11张C#读写INI文件第12张    
/**/////读取指定的Section的所有Value到列表中,
C#读写INI文件第15张//publicvoidReadSectionValues(stringSection,NameValueCollectionValues,charsplitString)
C#读写INI文件第15张    
//{ stringsectionValue;
C#读写INI文件第15张    
//string[]sectionValueSplit;
C#读写INI文件第15张    
//StringCollectionKeyList=newStringCollection();
C#读写INI文件第15张    
//ReadSection(Section,KeyList);
C#读写INI文件第15张    
//Values.Clear();
C#读写INI文件第15张    
//foreach(stringkeyinKeyList)
C#读写INI文件第15张    
//{
C#读写INI文件第15张    
//sectionValue=ReadString(Section,key,"");
C#读写INI文件第15张    
//sectionValueSplit=sectionValue.Split(splitString);
C#读写INI文件第15张    
//Values.Add(key,sectionValueSplit[0].ToString(),sectionValueSplit[1].ToString());
C#读写INI文件第15张 
C#读写INI文件第15张    
//}
C#读写INI文件第15张    
//}
C#读写INI文件第15张    
//清除某个Section
C#读写INI文件第15张
publicvoidEraseSection(stringSection)
C#读写INI文件第11张C#读写INI文件第12张    
C#读写INI文件第10张{
C#读写INI文件第15张      
//
C#读写INI文件第15张
if(!WritePrivateProfileString(Section,null,null,FileName))
C#读写INI文件第11张C#读写INI文件第12张      
C#读写INI文件第10张{
C#读写INI文件第15张
C#读写INI文件第15张        
throw(newApplicationException("无法清除Ini文件中的Section"));
C#读写INI文件第16张      }

C#读写INI文件第16张    }

C#读写INI文件第15张    
//删除某个Section下的键
C#读写INI文件第15张
publicvoidDeleteKey(stringSection,stringIdent)
C#读写INI文件第11张C#读写INI文件第12张    
C#读写INI文件第10张{
C#读写INI文件第15张      WritePrivateProfileString(Section,Ident,
null,FileName);
C#读写INI文件第16张    }

C#读写INI文件第15张    
//Note:对于Win9X,来说需要实现UpdateFile方法将缓冲中的数据写入文件
C#读写INI文件第15张    
//在WinNT,2000和XP上,都是直接写文件,没有缓冲,所以,无须实现UpdateFile
C#读写INI文件第15张    
//执行完对Ini文件的修改之后,应该调用本方法更新缓冲区。
C#读写INI文件第15张
publicvoidUpdateFile()
C#读写INI文件第11张C#读写INI文件第12张    
C#读写INI文件第10张{
C#读写INI文件第15张      WritePrivateProfileString(
null,null,null,FileName);
C#读写INI文件第16张    }

C#读写INI文件第15张
C#读写INI文件第15张    
//检查某个Section下的某个键值是否存在
C#读写INI文件第15张
publicboolValueExists(stringSection,stringIdent)
C#读写INI文件第11张C#读写INI文件第12张    
C#读写INI文件第10张{
C#读写INI文件第15张      
//
C#读写INI文件第15张
StringCollectionIdents=newStringCollection();
C#读写INI文件第15张      ReadSection(Section,Idents);
C#读写INI文件第15张      
returnIdents.IndexOf(Ident)>-1;
C#读写INI文件第16张    }

C#读写INI文件第15张
C#读写INI文件第15张    
//确保资源的释放
C#读写INI文件第15张
~IniFiles()
C#读写INI文件第11张C#读写INI文件第12张    
C#读写INI文件第10张{
C#读写INI文件第15张      UpdateFile();
C#读写INI文件第16张    }

C#读写INI文件第16张  }

C#读写INI文件第276张}

C#读写INI文件第1张

目前C#对ini文件操作基本上要被xml文件取代了,但是我觉得ini文件的读写仍然是编程的基本,是必须会的。

免责声明:文章转载自《C#读写INI文件》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇KEIL查看ARM-Cortex M架构soc的内核寄存器之 MSPmysql启动参数 skip-grant-tables下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

iOS-高仿通讯录之商品索引排序搜索

概述 TableView添加右侧索引, 将数据按照索引分组排序, 并添加搜索功能且在搜索界面复用当前页面. 详细 代码下载:http://www.demodashi.com/demo/10696.html 项目中像一些商品搜索类界面, TableView添加右侧索引的使用越来越多, 的确用户体验提高了许多. 一、主要思路 大致思路: 1....

iOS tableview上部白条在不同系统上的处理,

1.白条产生的原因: iOS 7 viewcontroller新增属性automaticallyAdjustsScrollViewInsets,即是否根据按所在界面的navigationbar与tabbar的高度,自动调整scrollview的 inset self.automaticallyAdjustsScrollViewInsets = NO; 2....

内核中的宏定义__init、__initdata和__exit、__exitdata

  __init、__initdata和__exit、__exitdata的定义位于<kernel/include/linux/init.h> 1 /* These are for everybody (although not all archs will actually 2 discard it in modules) */...

C#读写ini文件详解

  原文地址:http://developer.51cto.com/art/200908/143715.htm C#读写ini文件之前要了解的概念:INI就是扩展名为"INI"的文件,其实他本身是个文本文件,可以用记事本打开,主要存放的是用户所做的选择或系统的各种参数. C#读写ini文件其实并不是普通的文本文件.它有自己的结构.由若干段落(SECTION...

ELF格式探析之三:sections

前文链接: ELF格式探析之一:Segment和Section ELF格式探析之二:文件头ELF Header详解 今天我们讲对目标文件(可重定位文件)和可执行文件都很重要的section。 我们在讲ELF Header的时候,讲到了section header table。它是一个section header的集合,每个section header是...

IOS学习:UITableView使用详解3 分组表的简单使用

IOS学习:UITableView使用详解3 分组表的简单使用 使用分组表和使用普通表的方法差不多,他们的不同点有以下几点: 1.分组表的属性必须设置为Grouped的,而不是plain 2.分组表的数据源方法当中numberOfSectionsInTableView:返回分组的个数。 3.可以设置tableView:titleForHeaderInSec...