• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 众包
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录

wayde


博客园    首页    新随笔    联系   管理    订阅  订阅
ini文件的操作

using System;
using System.Text;
using System.Runtime.InteropServices;

public class INIHelper
{
    [DllImport(
"kernel32")]
    
private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);

    [DllImport(
"kernel32")]
    
private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);

    [DllImport(
"kernel32")]
    
private static extern int GetPrivateProfileSectionNames(byte[] retVal, int size, string filePath);

    
private readonly string strFileName;

    
public string FileName
    {
        
get{return strFileName;}
    }

    
public INIHelper(string Filename)
    {
        strFileName 
= System.IO.Path.GetFullPath(Filename);
    }

    
public byte[] GetSectionBytes()
    {
        
byte[] val = new byte[32768];
        
int size = GetPrivateProfileSectionNames(val, 32768, strFileName);
        
byte[] retval = new byte[size];
        Array.Copy(val, 
0, retval, 0, size);
        
return retval;
    }

    
public string[] Sections
    {
        
get
        {
            
byte[] sec = GetSectionBytes();
            
string strs = System.Text.Encoding.Default.GetString(sec, 0, sec.Length);
            
return strs.ToUpper().Split('\0');
        }
    }

    
public bool SectionExist(string Section)
    {
        Section 
= Section.Replace(" ", "");
        
if (Section.Equals(""))
        {
            
return false;
        }
        
else
        {
            
return Array.IndexOf(Sections, Section.ToUpper()) != -1;
        }
    }

    
public void RemoveSection(string Section)
    {
        
if (Section != null)
        {
            WritePrivateProfileString(Section, 
null, null, strFileName);
        }
    }

    
public void RemoveKey(string Section, string Key)
    {
        
if (Section != null && Key != null)
        {
            WritePrivateProfileString(Section, Key, 
null, strFileName);
        }
    }

    
public void Write(string Section, string Key, string Value)
    {
        
if (Section != null && Key != null && Value != null)
        {
            WritePrivateProfileString(Section, Key, Value, strFileName);
        }
    }

    
public void Write(string Section, string Key, float Value)
    {
        Write(Section, Key, Value.ToString());
    }

    
public void Write(string Section, string Key, int Value)
    {
        Write(Section, Key, Value.ToString());
    }

    
public void Write(string Section, string Key, bool Value)
    {
        Write(Section, Key, Value.ToString());
    }

    
public string Read(string Section, string Key, string DefaultValue)
    {
        StringBuilder val 
= new StringBuilder(32768);
        GetPrivateProfileString(Section, Key, DefaultValue, val, 
8192, strFileName);
        
if (Section.Equals(null) || Key.Equals(null))
        {
            
return string.Empty;
        }
        
return val.ToString();
    }

    
public bool Read(string Section, string Key, bool DefaultValue)
    {
        
string defaultvalue = "False";
        
bool retvalue = false;
        
try
        {
            defaultvalue 
= DefaultValue.ToString();
            retvalue 
= bool.Parse(Read(Section, Key, defaultvalue));
        }
        
catch
        {
        }
        
return retvalue;
    }

    
public int Read(string Section, string Key, int DefaultValue)
    {
        
string defaultvalue = "0";
        
int retvalue = 0;
        
try
        {
            defaultvalue 
= DefaultValue.ToString();
            retvalue 
= int.Parse(Read(Section, Key, defaultvalue));
        }
        
catch
        {
        }
        
return retvalue;
    }

    
public float Read(string Section, string Key, float DefaultValue)
    {
        
string defaultvalue = "0";
        
float retvalue = 0;
        
try
        {
            defaultvalue 
= DefaultValue.ToString();
            retvalue 
= float.Parse(Read(Section, Key, defaultvalue));
        }
        
catch
        {
        }
        
return retvalue;
    }
}


posted on 2007-09-17 17:32  wayde  阅读(195)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3