C#调用win32 API读写INI文件

本文介绍了一个用于操作INI文件的C#类,包括如何设置和获取INI文件中的键值对,以及资源管理和垃圾回收的相关实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
namespace INI
{
    class ClassINI:IDisposable 
    {
        [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 ref Val, int size, string filePath);

        private bool bDisposed = false;
        private string _FilePath = string.Empty;
        public string FilePath
        {
            get
            {
                if (_FilePath == null)
                    return string.Empty;
                else
                    return _FilePath;
            }
            set
            {
                if (_FilePath != value)
                    _FilePath = value;
            }
        }
        ///<summary>
        ///構造函數
        ///</summary>
        ///<param name="path">檔案路徑</param>
        public ClassINI(string path)
        {
            _FilePath = path;
        }
        ///<summary>
        ///析構函數
        ///</summary>
        ~ClassINI()
        {
            Dispose(false);
        }

        ///<summary>
        ///釋放資源(程序設計師呼叫)
        ///</summary>
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);    //要求系統不要呼叫指定物件的完成項
        }
        ///<summary>
        ///釋放資源(給系統呼叫的)
        ///</summary>
        protected virtual void Dispose(bool isDisposing)
        {
            if (bDisposed)
            {
                return;
            }
            if (isDisposing)
            {

            }
            bDisposed = true;
        }

        ///<summary>
        ///設定KeyValue值
        ///</summary>
        ///<param name="IN_Section">Section</param>
        ///<param name="IN_Key">Key</param>
        ///<param name="IN_Value">Value</param>
        public void setKeyValue(string IN_Section,string IN_Key,string IN_Value)
        {
            WritePrivateProfileString(IN_Section, IN_Key, IN_Value, this._FilePath);
        }

        ///<summary>
        ///取得Key相對的Value值
        ///</summary>
        ///<param name="IN_Section">Section</param>
        ///<param name="IN_Key">Key</param>
        public string getKeyValue(string IN_Section, string IN_Key)
        {
            StringBuilder temp = new StringBuilder(255);
            int i = GetPrivateProfileString(IN_Section, IN_Key, "", temp, 255, this._FilePath);
            return temp.ToString();
        }

        ///<summary>
        ///取得key相對的value值,若無,則使用預設值(DefaulValue)
        ///</summary>
        ///<param name="Section">Section</param>
        ///<param name="Key">Key</param>
        ///<param name="DefaultValue">DefaultValue</param>
        public string getKeyValue(string Section, string Key, string DefaultValue)
        {
            StringBuilder sbResult = null;

            try
            {
                sbResult = new StringBuilder(255);
                GetPrivateProfileString(Section, Key, "", sbResult, 255, this._FilePath);
                return (sbResult.Length > 0) ? sbResult.ToString() : DefaultValue;
            }
            catch
            {
                return string.Empty;
            }
        }
    }
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值