[转]VB 读写ini 配置文件

本文介绍如何使用C#语言通过API函数实现INI配置文件的读取与写入操作。提供了具体的函数声明及使用方法,并附有示例代码。

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

转自 百度知道

C# 读写 ini配置文件 点此链接

'API 声明
Public Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
Public Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long
'声明结束

Public Function WriteToIni(ByVal FileName As String, ByVal Section As String, ByVal Key As String, ByVal Value As String) As Boolean
Dim buff As String * 128
buff = Value + Chr(0)
WriteToIni = IIf(WritePrivateProfileString(Section, Key, buff, FileName) = 1, True, False)
End Function
'写入(Section不带中括号),Value是写入值(最好先CStr成String),返回“True”写入成功,反之失败

Public Function ReadFromIni(ByVal FileName As String, ByVal Section As String, ByVal Key As String) As String
Dim I As Long
Dim buff As String * 128
If GetPrivateProfileString(Section, Key, "", buff, 128, FileName) = 0 Then ReadFromIni = "": Exit Function
I = InStr(buff, Chr(0))
ReadFromIni = Trim(Left(buff, I - 1))
End Function

转载于:https://www.cnblogs.com/z5337/p/3689308.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值