调用API函数读写INI

本文详细介绍了如何通过API函数来读取和写入INI配置文件,包括关键步骤、函数调用及示例代码,帮助开发者理解如何在程序中灵活地管理配置参数。
摘要由CSDN通过智能技术生成

 

using  System;
using  System.Collections.Generic;
using  System.Text;
using  System.Runtime.InteropServices;

namespace  Config
{
    
class PrivateProfile
    
{
        
/// <summary>
        
/// 添加GetPrivateProfileInt等API函数的声明
        
/// </summary>

        
        
//UINT GetPrivateProfileInt(
        
//  LPCTSTR lpAppName,
        
//  LPCTSTR lpKeyName,
        
//  INT nDefault,
        
//  LPCTSTR lpFileName
        
//);
        [DllImport("kernel32", EntryPoint = "GetPrivateProfileInt")]
        
public static extern int GetInt(
            
string lpAppName,
            
string lpKeyName,
            
int nDefault,
            
string lpFileName);

        
//DWORD GetPrivateProfileSection(
        
//  LPCTSTR lpAppName,
        
//  LPTSTR lpReturnedString,
        
//  DWORD nSize,
        
//  LPCTSTR lpFileName
        
//);
        [DllImport("kernel32", EntryPoint = "GetPrivateProfileSection")]
        
public static extern int GetSection(
            
string lpAppName,
            StringBuilder lpReturnedString,
            
int nSize,
            
string lpFileName);

        
//DWORD GetPrivateProfileSectionNames(
        
//  LPTSTR lpszReturnBuffer,
        
//  DWORD nSize,
        
//  LPCTSTR lpFileName
        
//);
        [DllImport("kernel32", EntryPoint = "GetPrivateProfileSectionNames")]
        
public static extern int GetSectionNames(
            StringBuilder lpszReturnBuffer,
            
int nSize,
            
string lpFileName);

        
//DWORD GetPrivateProfileString(
        
//  LPCTSTR lpAppName,
        
//  LPCTSTR lpKeyName,
        
//  LPCTSTR lpDefault,
        
//  LPTSTR lpReturnedString,
        
//  DWORD nSize,
        
//  LPCTSTR lpFileName
        
//);
        [DllImport("kernel32", EntryPoint = "GetPrivateProfileString")]
        
public static extern int GetString(
            
string lpAppName,
            
string lpKeyName,
            
string lpDefault,
            StringBuilder lpReturnedString,
            
int nSize,
            
string lpFileName);

        
//BOOL GetPrivateProfileStruct(
        
//  LPCTSTR lpszSection,
        
//  LPCTSTR lpszKey,
        
//  LPVOID lpStruct,
        
//  UINT uSizeStruct,
        
//  LPCTSTR szFile
        
//);
        [DllImport("kernel32", EntryPoint = "GetPrivateProfileStruct")]
        
unsafe public static extern bool GetStruct(
          
string lpszSection,
          
string lpszKey,
          
void* lpStruct,
          
uint uSizeStruct,
          
string szFile
        );

        
//BOOL WritePrivateProfileSection(
        
//  LPCTSTR lpAppName,
        
//  LPCTSTR lpString,
        
//  LPCTSTR lpFileName
        
//);
        [DllImport("kernel32",EntryPoint = "WritePrivateProfileSection" )]
        
public static extern bool WriteSection(
            
string lpAppName,
            
string lpString,
            
string lpFileName);

        
// BOOL WritePrivateProfileString(
        
//  LPCTSTR lpAppName,
        
//  LPCTSTR lpKeyName,
        
//  LPCTSTR lpString,
        
//  LPCTSTR lpFileName
        
//);
        [DllImport("kernel32", EntryPoint = "WritePrivateProfileString")]
        
public static extern bool WriteString(
            
string lpAppName,
            
string lpKeyName,
            
string lpString,
            
string lpFileName);

        
//BOOL WritePrivateProfileStruct(
        
//  LPCTSTR lpszSection,
        
//  LPCTSTR lpszKey,
        
//  LPVOID lpStruct,
        
//  UINT uSizeStruct,
        
//  LPCTSTR szFile
        
//);
        [DllImport("kernel32", EntryPoint = "WritePrivateProfileStruct")]
        
unsafe public static extern bool WriteStruct(
            
string lpszSection,
            
string lpszKey,
            
void* lpStruct,
            
uint uSizeStruct,
            
string szFile);
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值