VB编程:删除INI配置文件中某小节名下的全部文本内容

该篇文章介绍了如何使用一个名为`delSectionAllTxtInINIFile`的PublicFunction,它处理INI文件中指定SectionName下的内容,可以选择性地删除小节名。函数接收SectionName、INI文件路径和一个可选参数来决定是否删除小节名。
摘要由CSDN通过智能技术生成

'参数:SectionName  '小节名,前后可有[],也可无
'参数:optDelIncludeSectionName_1 '默认为1,表示同时将小节名删除,为0则保留小节名不删,只删除小节名下的全部Key
'实例:delSectionAllTxtInINIFile  "[登录帐户XX]", "S:\帐户.ini"   '
'      delSectionAllTxtInINIFile  "登录帐户XX", "S:\帐户.ini", 0  '保留小节名不删
Public Function delSectionAllTxtInINIFile(SectionName, INIFile, Optional optDelIncludeSectionName_1 = 1)
    Dim ColectStr As String
    If FileExist(INIFile) Then
        If Left$(SectionName, 1) <> "[" And Right$(SectionName, 1) <> "]" Then
            SectionName = "[" & SectionName & "]"
        End If

        TmpFileNum1 = FreeFile
        Open INIFile For Input As #TmpFileNum1
        Do While Not EOF(TmpFileNum1)
            Line Input #TmpFileNum1, TmpStr$
            If Trim$(TmpStr$) = SectionName Then
                If optDelIncludeSectionName_1 <> 1 Then
                    ColectStr = ColectStr & TmpStr$ & vbCrLf   '不删小节名
                End If
                Do While Not EOF(TmpFileNum1)
                    Line Input #TmpFileNum1, TmpStr$
                    If TmpStr$ <> "" Then
                        LsTmpStr$ = Trim$(TmpStr$)
                        If Left$(LsTmpStr$, 1) = "[" And Right$(LsTmpStr$, 1) = "]" Then   '下一节了
                            ColectStr = ColectStr & TmpStr$ & vbCrLf
                            Exit Do
                        End If
                    Else
                        ColectStr = ColectStr & TmpStr$ & vbCrLf
                    End If
                Loop
            Else
                ColectStr = ColectStr & TmpStr$ & vbCrLf
            End If
        Loop
        Close #TmpFileNum1
        DeleteFile INIFile
        If ColectStr <> "" Then
            If Right$(ColectStr, 2) = vbCrLf Then
                ColectStr = Left$(ColectStr, Len(ColectStr) - 2)
            End If
            AppendWriteFile ColectStr, INIFile
        End If
    End If
End Function

  • 9
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值