① VB.NET2010读取INI配置文件问题
设计一个读取函数
说明:在引用的面页中要先引用 Imports System.IO
Function sdGetIniInfo(ByVal iniFile As String, ByVal iniSection As String) As String
If Not File.Exists(iniFile) Then
Return "文件 " & iniFile & " 未找到,请确认路径和文件名是否正确!"
Exit Function
End If
Dim iniRead As New StreamReader(iniFile) Dim iniStr As String = iniRead.ReadToEnd
Dim i As Integer
Dim cLine As Integer
Dim noSec As Boolean = False
Dim getValue As String = ""
Dim cLst
cLst = iniStr.Split(Chr(13)) cLine = UBound(cLst)
For i = 0 To cLine
If cLst(i).indexof("=") > 0 Then
If cLst(i).split("=")(0).trim() = iniSection Then
noSec = True
getValue = cLst(i).split("=")(1).trim()
Exit For
End If
End If
Next
If noSec = True Then
Return getValue
Else
Return "没有找到 " & iniSection & " 的设置信息!"
End If
End Function
==========================================
TEXT1=sdGetIniInfo(路径,“Name”)
其他类似
其实,无论ini和xml都是文本文件,只是带了一些格式,或者叫数据结构吧,归根到底都是用文件操作来实现,一些函数也只不过是封装好了的文件读写操作而已!
这不是api,就是自己写个函数!
② VB.NET怎么删除INI文件里的一个字段或键值
1,对于INI文件,可以当做像TXT文件一样来进行读取和写入。
2,先把整个文件度出来,然后找到相应行删除(抛弃)以后,再重新写入文件。
PublicClassForm1
PrivateSubButton1_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)HandlesButton1.Click
DimMyStrAsString=""
DimAllStrAsString=""
'获取一个可用的文件号
DimMyFileNumAsInteger=FreeFile()
'打开指定的文件,进行读取操作
FileOpen(MyFileNum,"C:My.ini",OpenMode.Input)
DoWhileNotEOF(MyFileNum)
'读取一行
MyStr=LineInput(MyFileNum)
IfMyStr<>"b=2"Then
IfAllStr=""Then
AllStr=AllStr&MyStr
Else
AllStr=AllStr&vbCrLf&MyStr
EndIf
EndIf
Loop
FileClose(MyFileNum)'关闭文件
'写文件
DimMyStreamAsNewSystem.IO.FileStream("C:My.ini",IO.FileMode.Create)
DimMyWriterAsNewSystem.IO.StreamWriter(MyStream,System.Text.Encoding.UTF8)
MyWriter.WriteLine(AllStr)
MyWriter.Flush()
MyWriter.Close()
MyStream.Close()
EndSub
EndClass