C# ini文件读写 类

一个C# ini文件读写类,从网上收集的,很全,就是没有对section的改名功能,高手可以增加一个

None.gif using  System;
None.gif
using  System.IO;
None.gif
using  System.Runtime.InteropServices;
None.gif
using  System.Text;
None.gif
using  System.Collections;
None.gif
using  System.Collections.Specialized;
None.gif
None.gif
namespace  wuyiskyExpandedBlockStart.gifContractedBlock.gif dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//**/
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary> 
InBlock.gif    
/// IniFiles的类 
ExpandedSubBlockEnd.gif    
/// </summary> 

InBlock.gif    public class IniFiles
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public string FileName; //INI文件名 
InBlock.gif        
//声明读写INI文件的API函数 
InBlock.gif
        [DllImport("kernel32")]
InBlock.gif        
private static extern bool WritePrivateProfileString(string section, string key, string val, string filePath);
InBlock.gif        [DllImport(
"kernel32")]
InBlock.gif        
private static extern int GetPrivateProfileString(string section, string key, string def, byte[] retVal, int size, string filePath);
InBlock.gif        
//类的构造函数,传递INI文件名 
InBlock.gif
        public IniFiles(string AFileName)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
// 判断文件是否存在 
InBlock.gif
            FileInfo fileInfo = new FileInfo(AFileName);
InBlock.gif            
//Todo:搞清枚举的用法 
InBlock.gif
            if ((!fileInfo.Exists))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif//|| (FileAttributes.Directory in fileInfo.Attributes)) 
InBlock.gif                
//文件不存在,建立文件 
InBlock.gif
                System.IO.StreamWriter sw = new System.IO.StreamWriter(AFileName, false, System.Text.Encoding.Default);
InBlock.gif                
try
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    sw.Write(
"#表格配置档案");
InBlock.gif                    sw.Close();
ExpandedSubBlockEnd.gif                }

InBlock.gif                
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
throw (new ApplicationException("Ini文件不存在"));
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
//必须是完全路径,不能是相对路径 
InBlock.gif
            FileName = fileInfo.FullName;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
//写INI文件 
InBlock.gif
        public void WriteString(string Section, string Ident, string Value)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (!WritePrivateProfileString(Section, Ident, Value, FileName))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif               
InBlock.gif                
throw (new ApplicationException("写Ini文件出错"));
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
//读取INI文件指定 
InBlock.gif
        public string ReadString(string Section, string Ident, string Default)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Byte[] Buffer 
= new Byte[65535];
InBlock.gif            
int bufLen = GetPrivateProfileString(Section, Ident, Default, Buffer, Buffer.GetUpperBound(0), FileName);
InBlock.gif            
//必须设定0(系统默认的代码页)的编码方式,否则无法支持中文 
InBlock.gif
            string s = Encoding.GetEncoding(0).GetString(Buffer);
InBlock.gif            s 
= s.Substring(0, bufLen);
InBlock.gif            
return s.Trim();
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
//读整数 
InBlock.gif
        public int ReadInteger(string Section, string Ident, int Default)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string intStr = ReadString(Section, Ident, Convert.ToString(Default));
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return Convert.ToInt32(intStr);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch (Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Console.WriteLine(ex.Message);
InBlock.gif                
return Default;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
//写整数 
InBlock.gif
        public void WriteInteger(string Section, string Ident, int Value)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            WriteString(Section, Ident, Value.ToString());
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
//读布尔 
InBlock.gif
        public bool ReadBool(string Section, string Ident, bool Default)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return Convert.ToBoolean(ReadString(Section, Ident, Convert.ToString(Default)));
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch (Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Console.WriteLine(ex.Message);
InBlock.gif                
return Default;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
//写Bool 
InBlock.gif
        public void WriteBool(string Section, string Ident, bool Value)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            WriteString(Section, Ident, Convert.ToString(Value));
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
//从Ini文件中,将指定的Section名称中的所有Ident添加到列表中 
InBlock.gif
        public void ReadSection(string Section, StringCollection Idents)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Byte[] Buffer 
= new Byte[16384];
InBlock.gif            
//Idents.Clear(); 
InBlock.gif

InBlock.gif            
int bufLen = GetPrivateProfileString(Section, nullnull, Buffer, Buffer.GetUpperBound(0),
InBlock.gif              FileName);
InBlock.gif            
//对Section进行解析 
InBlock.gif
            GetStringsFromBuffer(Buffer, bufLen, Idents);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void GetStringsFromBuffer(Byte[] Buffer, int bufLen, StringCollection Strings)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Strings.Clear();
InBlock.gif            
if (bufLen != 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
int start = 0;
InBlock.gif                
for (int i = 0; i < bufLen; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
if ((Buffer[i] == 0&& ((i - start) > 0))
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        String s 
= Encoding.GetEncoding(0).GetString(Buffer, start, i - start);
InBlock.gif                        Strings.Add(s);
InBlock.gif                        start 
= i + 1;
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
//从Ini文件中,读取所有的Sections的名称 
InBlock.gif
        public void ReadSections(StringCollection SectionList)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//Note:必须得用Bytes来实现,StringBuilder只能取到第一个Section 
InBlock.gif
            byte[] Buffer = new byte[65535];
InBlock.gif            
int bufLen = 0;
InBlock.gif            bufLen 
= GetPrivateProfileString(nullnullnull, Buffer,
InBlock.gif              Buffer.GetUpperBound(
0), FileName);
InBlock.gif            GetStringsFromBuffer(Buffer, bufLen, SectionList);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
//读取指定的Section的所有Value到列表中 
InBlock.gif
        public void ReadSectionValues(string Section, NameValueCollection Values)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            StringCollection KeyList 
= new StringCollection();
InBlock.gif            ReadSection(Section, KeyList);
InBlock.gif            Values.Clear();
InBlock.gif            
foreach (string key in KeyList)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Values.Add(key, ReadString(Section, key, 
""));
InBlock.gif                
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**/////读取指定的Section的所有Value到列表中, 
InBlock.gif        //public void ReadSectionValues(string Section, NameValueCollection Values,char splitString)
InBlock.gif        
//{   string sectionValue;
InBlock.gif        
//    string[] sectionValueSplit;
InBlock.gif        
//    StringCollection KeyList = new StringCollection();
InBlock.gif        
//    ReadSection(Section, KeyList);
InBlock.gif        
//    Values.Clear();
InBlock.gif        
//    foreach (string key in KeyList)
InBlock.gif        
//    {   
InBlock.gif        
//        sectionValue=ReadString(Section, key, "");
InBlock.gif        
//        sectionValueSplit=sectionValue.Split(splitString);
InBlock.gif        
//        Values.Add(key, sectionValueSplit[0].ToString(),sectionValueSplit[1].ToString());
InBlock.gif               
InBlock.gif        
//    }
InBlock.gif        
//}
InBlock.gif        
//清除某个Section 
InBlock.gif
        public void EraseSection(string Section)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
// 
InBlock.gif
            if (!WritePrivateProfileString(Section, nullnull, FileName))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
throw (new ApplicationException("无法清除Ini文件中的Section"));
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
//删除某个Section下的键 
InBlock.gif
        public void DeleteKey(string Section, string Ident)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            WritePrivateProfileString(Section, Ident, 
null, FileName);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
//Note:对于Win9X,来说需要实现UpdateFile方法将缓冲中的数据写入文件 
InBlock.gif        
//在Win NT, 2000和XP上,都是直接写文件,没有缓冲,所以,无须实现UpdateFile 
InBlock.gif        
//执行完对Ini文件的修改之后,应该调用本方法更新缓冲区。 
InBlock.gif
        public void UpdateFile()
html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值