C#读写INI文件

释放双眼,带上耳机,听听看~!

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

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

[Section]

Key=Value

      

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

C#读写INI文件
using
 System;
C#读写INI文件
using
 System.IO;
C#读写INI文件
using
 System.Runtime.InteropServices;
C#读写INI文件
using
 System.Text;
C#读写INI文件
using
 System.Collections;
C#读写INI文件
using
 System.Collections.Specialized;
C#读写INI文件
C#读写INI文件C#读写INI文件
namespace
 wuyisky
C#读写INI文件
{
C#读写INI文件C#读写INI文件  
/**/
/**/
C#读写INI文件C#读写INI文件  
/**/
///
 

<summary>
![](https://aqzt.com/wp-content/uploads/20220223054149-30.gif)  
///
 IniFiles的类
![](https://aqzt.com/wp-content/uploads/20220223054149-74.gif)  
///
 
</summary>

C#读写INI文件

public
 
class
 IniFiles
C#读写INI文件C#读写INI文件  
C#读写INI文件
{
C#读写INI文件    
public
 
string
 FileName; 
//
INI文件名
C#读写INI文件    
//
声明读写INI文件的API函数
C#读写INI文件
[DllImport(
"
kernel32
"
)]
C#读写INI文件    
private
 
static
 
extern
 
bool
 WritePrivateProfileString(
string
 section, 
string
 key, 
string
 val, 
string
 filePath);
C#读写INI文件    [DllImport(
"
kernel32
"
)]
C#读写INI文件    
private
 
static
 
extern
 
int
 GetPrivateProfileString(
string
 section, 
string
 key, 
string
 def, 
byte
[] retVal, 
int
 size, 
string
 filePath);
C#读写INI文件    
//
类的构造函数,传递INI文件名
C#读写INI文件

public
 IniFiles(
string
 AFileName)
C#读写INI文件C#读写INI文件    
C#读写INI文件
{
C#读写INI文件      
//
 判断文件是否存在
C#读写INI文件
FileInfo fileInfo 

 
new
 FileInfo(AFileName);
C#读写INI文件      
//
Todo:搞清枚举的用法
C#读写INI文件

if
 ((
!
fileInfo.Exists))
C#读写INI文件C#读写INI文件      
C#读写INI文件

//
|| (FileAttributes.Directory in fileInfo.Attributes))
C#读写INI文件        
//
文件不存在,建立文件
C#读写INI文件
System.IO.StreamWriter sw 

 
new
 System.IO.StreamWriter(AFileName, 
false
, System.Text.Encoding.Default);
C#读写INI文件        
try
C#读写INI文件C#读写INI文件        
C#读写INI文件
{
C#读写INI文件          sw.Write(
"
#表格配置档案
"
);
C#读写INI文件          sw.Close();
C#读写INI文件        }
C#读写INI文件
C#读写INI文件        
catch
C#读写INI文件C#读写INI文件        
C#读写INI文件
{
C#读写INI文件          
throw
 (
new
 ApplicationException(
"
Ini文件不存在
"
));
C#读写INI文件        }
C#读写INI文件      }
C#读写INI文件      
//
必须是完全路径,不能是相对路径
C#读写INI文件
FileName 

 fileInfo.FullName;
C#读写INI文件    }
C#读写INI文件    
//
写INI文件
C#读写INI文件

public
 
void
 WriteString(
string
 Section, 
string
 Ident, 
string
 Value)
C#读写INI文件C#读写INI文件    
C#读写INI文件
{
C#读写INI文件      
if
 (
!
WritePrivateProfileString(Section, Ident, Value, FileName))
C#读写INI文件C#读写INI文件      
C#读写INI文件
{
C#读写INI文件 
C#读写INI文件        
throw
 (
new
 ApplicationException(
"
写Ini文件出错
"
));
C#读写INI文件      }
C#读写INI文件    }
C#读写INI文件    
//
读取INI文件指定
C#读写INI文件

public
 
string
 ReadString(
string
 Section, 
string
 Ident, 
string
 Default)
C#读写INI文件C#读写INI文件    
C#读写INI文件
{
C#读写INI文件      Byte[] Buffer 

 
new
 Byte[
65535
];
C#读写INI文件      
int
 bufLen 

 GetPrivateProfileString(Section, Ident, Default, Buffer, Buffer.GetUpperBound(
0
), FileName);
C#读写INI文件      
//
必须设定0(系统默认的代码页)的编码方式,否则无法支持中文
C#读写INI文件

string
 s 

 Encoding.GetEncoding(
0
).GetString(Buffer);
C#读写INI文件      s 

 s.Substring(
0
, bufLen);
C#读写INI文件      
return
 s.Trim();
C#读写INI文件    }
C#读写INI文件
C#读写INI文件    
//
读整数
C#读写INI文件

public
 
int
 ReadInteger(
string
 Section, 
string
 Ident, 
int
 Default)
C#读写INI文件C#读写INI文件    
C#读写INI文件
{
C#读写INI文件      
string
 intStr 

 ReadString(Section, Ident, Convert.ToString(Default));
C#读写INI文件      
try
C#读写INI文件C#读写INI文件      
C#读写INI文件
{
C#读写INI文件        
return
 Convert.ToInt32(intStr);
C#读写INI文件
C#读写INI文件      }
C#读写INI文件      
catch
 (Exception ex)
C#读写INI文件C#读写INI文件      
C#读写INI文件
{
C#读写INI文件        Console.WriteLine(ex.Message);
C#读写INI文件        
return
 Default;
C#读写INI文件      }
C#读写INI文件    }
C#读写INI文件
C#读写INI文件    
//
写整数
C#读写INI文件

public
 
void
 WriteInteger(
string
 Section, 
string
 Ident, 
int
 Value)
C#读写INI文件C#读写INI文件    
C#读写INI文件
{
C#读写INI文件      WriteString(Section, Ident, Value.ToString());
C#读写INI文件    }
C#读写INI文件
C#读写INI文件    
//
读布尔
C#读写INI文件

public
 
bool
 ReadBool(
string
 Section, 
string
 Ident, 
bool
 Default)
C#读写INI文件C#读写INI文件    
C#读写INI文件
{
C#读写INI文件      
try
C#读写INI文件C#读写INI文件      
C#读写INI文件
{
C#读写INI文件        
return
 Convert.ToBoolean(ReadString(Section, Ident, Convert.ToString(Default)));
C#读写INI文件      }
C#读写INI文件      
catch
 (Exception ex)
C#读写INI文件C#读写INI文件      
C#读写INI文件
{
C#读写INI文件        Console.WriteLine(ex.Message);
C#读写INI文件        
return
 Default;
C#读写INI文件      }
C#读写INI文件    }
C#读写INI文件
C#读写INI文件    
//
写Bool
C#读写INI文件

public
 
void
 WriteBool(
string
 Section, 
string
 Ident, 
bool
 Value)
C#读写INI文件C#读写INI文件    
C#读写INI文件
{
C#读写INI文件      WriteString(Section, Ident, Convert.ToString(Value));
C#读写INI文件    }
C#读写INI文件
C#读写INI文件    
//
从Ini文件中,将指定的Section名称中的所有Ident添加到列表中
C#读写INI文件

public
 
void
 ReadSection(
string
 Section, StringCollection Idents)
C#读写INI文件C#读写INI文件    
C#读写INI文件
{
C#读写INI文件      Byte[] Buffer 

 
new
 Byte[
16384
];
C#读写INI文件      
//
Idents.Clear();
C#读写INI文件
C#读写INI文件      
int
 bufLen 

 GetPrivateProfileString(Section, 
null

null
, Buffer, Buffer.GetUpperBound(
0
),
C#读写INI文件       FileName);
C#读写INI文件      
//
对Section进行解析
C#读写INI文件
GetStringsFromBuffer(Buffer, bufLen, Idents);
C#读写INI文件    }
C#读写INI文件
C#读写INI文件    
private
 
void
 GetStringsFromBuffer(Byte[] Buffer, 
int
 bufLen, StringCollection Strings)
C#读写INI文件C#读写INI文件    
C#读写INI文件
{
C#读写INI文件      Strings.Clear();
C#读写INI文件      
if
 (bufLen 
!=
 
0
)
C#读写INI文件C#读写INI文件      
C#读写INI文件
{
C#读写INI文件        
int
 start 

 
0
;
C#读写INI文件        
for
 (
int
 i 

 
0
; i 
<
 bufLen; i
++
)
C#读写INI文件C#读写INI文件        
C#读写INI文件
{
C#读写INI文件          
if
 ((Buffer[i] 

 
0

&&
 ((i 

 start) 

 
0
))
C#读写INI文件C#读写INI文件          
C#读写INI文件
{
C#读写INI文件            String s 

 Encoding.GetEncoding(
0
).GetString(Buffer, start, i 

 start);
C#读写INI文件            Strings.Add(s);
C#读写INI文件            start 

 i 
+
 
1
;
C#读写INI文件          }
C#读写INI文件        }
C#读写INI文件      }
C#读写INI文件    }
C#读写INI文件    
//
从Ini文件中,读取所有的Sections的名称
C#读写INI文件

public
 
void
 ReadSections(StringCollection SectionList)
C#读写INI文件C#读写INI文件    
C#读写INI文件
{
C#读写INI文件      
//
Note:必须得用Bytes来实现,StringBuilder只能取到第一个Section
C#读写INI文件

byte
[] Buffer 

 
new
 
byte
[
65535
];
C#读写INI文件      
int
 bufLen 

 
0
;
C#读写INI文件      bufLen 

 GetPrivateProfileString(
null

null

null
, Buffer,
C#读写INI文件       Buffer.GetUpperBound(
0
), FileName);
C#读写INI文件      GetStringsFromBuffer(Buffer, bufLen, SectionList);
C#读写INI文件    }
C#读写INI文件    
//
读取指定的Section的所有Value到列表中
C#读写INI文件

public
 
void
 ReadSectionValues(
string
 Section, NameValueCollection Values)
C#读写INI文件C#读写INI文件    
C#读写INI文件
{
C#读写INI文件      StringCollection KeyList 

 
new
 StringCollection();
C#读写INI文件      ReadSection(Section, KeyList);
C#读写INI文件      Values.Clear();
C#读写INI文件      
foreach
 (
string
 key 
in
 KeyList)
C#读写INI文件C#读写INI文件      
C#读写INI文件
{
C#读写INI文件        Values.Add(key, ReadString(Section, key, 
""
));
C#读写INI文件  
C#读写INI文件      }
C#读写INI文件    }
C#读写INI文件C#读写INI文件    
/**/
///
/读取指定的Section的所有Value到列表中,

C#读写INI文件

//
public void ReadSectionValues(string Section, NameValueCollection Values,char splitString)
C#读写INI文件    
//
{  string sectionValue;
C#读写INI文件    
//
string[] sectionValueSplit;
C#读写INI文件    
//
StringCollection KeyList = new StringCollection();
C#读写INI文件    
//
ReadSection(Section, KeyList);
C#读写INI文件    
//
Values.Clear();
C#读写INI文件    
//
foreach (string key in KeyList)
C#读写INI文件    
//
{
C#读写INI文件    
//
sectionValue=ReadString(Section, key, "");
C#读写INI文件    
//
sectionValueSplit=sectionValue.Split(splitString);
C#读写INI文件    
//
Values.Add(key, sectionValueSplit[0].ToString(),sectionValueSplit[1].ToString());
C#读写INI文件 
C#读写INI文件    
//
}
C#读写INI文件    
//
}
C#读写INI文件    
//
清除某个Section
C#读写INI文件

public
 
void
 EraseSection(
string
 Section)
C#读写INI文件C#读写INI文件    
C#读写INI文件
{
C#读写INI文件      
//
C#读写INI文件

if
 (
!
WritePrivateProfileString(Section, 
null

null
, FileName))
C#读写INI文件C#读写INI文件      
C#读写INI文件
{
C#读写INI文件
C#读写INI文件        
throw
 (
new
 ApplicationException(
"
无法清除Ini文件中的Section
"
));
C#读写INI文件      }
C#读写INI文件    }
C#读写INI文件    
//
删除某个Section下的键
C#读写INI文件

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

public
 
void
 UpdateFile()
C#读写INI文件C#读写INI文件    
C#读写INI文件
{
C#读写INI文件      WritePrivateProfileString(
null

null

null
, FileName);
C#读写INI文件    }
C#读写INI文件
C#读写INI文件    
//
检查某个Section下的某个键值是否存在
C#读写INI文件

public
 
bool
 ValueExists(
string
 Section, 
string
 Ident)
C#读写INI文件C#读写INI文件    
C#读写INI文件
{
C#读写INI文件      
//
C#读写INI文件
StringCollection Idents 

 
new
 StringCollection();
C#读写INI文件      ReadSection(Section, Idents);
C#读写INI文件      
return
 Idents.IndexOf(Ident) 

 

1
;
C#读写INI文件    }
C#读写INI文件
C#读写INI文件    
//
确保资源的释放
C#读写INI文件

~
IniFiles()
C#读写INI文件C#读写INI文件    
C#读写INI文件
{
C#读写INI文件      UpdateFile();
C#读写INI文件    }
C#读写INI文件  }
C#读写INI文件}
C#读写INI文件
目前C# 对ini文件操作基本上要被xml文件取代了,但是我觉得ini文件的读写仍然是编程的基本,是必须会的

给TA打赏
共{{data.count}}人
人已打赏
安全技术

网站制作需要素材的实用网站

2021-12-21 16:36:11

安全技术

从零搭建自己的SpringBoot后台框架(二十三)

2022-1-12 12:36:11

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索