重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
以下是我的办法:OK!
成都创新互联公司主营自贡网站建设的网络公司,主营网站建设方案,成都App制作,自贡h5小程序开发搭建,自贡网站营销推广欢迎自贡等地区企业咨询
Public Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Integer, ByVal lpFileName As String) As Integer
Public Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpString As String, ByVal lpFileName As String) As Integer
写:
WritePrivateProfileString("存储", "账号", textbox1, Application.StartupPath "\song.ini")
读:
dim s as string
s = string(255," ")
GetPrivateProfileString("存储", "账号", s, s.Length, Application.StartupPath + "\song.ini")
textbox1 =s
vb.net有自己的设置,不需要ini,在工程【属性】-【设置】中设置。
——————
ini设置属性。
'声明API函数
Private Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpString As String, ByVal lpFileName As String) As Int32
Public Function WriteINI(ByVal Section As String, ByVal AppName As String, ByVal lpDefault As String, ByVal FileName As String) As Long
Return WritePrivateProfileString(Section, AppName, lpDefault, FileName)
End Function
‘调用writeini函数存储属性。
‘section为节点,也就是ini中带[]符号的节点文本
’APPname为存储的名称,即ini文件中等于符号前面的文本
’ipdefault为存储的属性,即等于符号右边的文本。
‘filename为存储的ini文件地址,如d:\save.ini
————————
ini返回属性。
'声明API函数
Private Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Int32, ByVal lpFileName As String) As Int32
Private Function GetINI(ByVal Section As String, ByVal AppName As String, ByVal lpDefault As String, ByVal FileName As String) As String
Dim Str As String = ""
Str = LSet(Str, IniCount)
GetPrivateProfileString(Section, AppName, lpDefault, Str, Len(Str), FileName)
Return Microsoft.VisualBasic.Left(Str, InStr(Str, Chr(0)) - 1)
End Function
’调用getini函数
‘说明同上。
1,对于INI文件,可以当做像TXT文件一样来进行读取和写入。
2,先把整个文件度出来,然后找到相应行删除(抛弃)以后,再重新写入文件。
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim MyStr As String = ""
Dim AllStr As String = ""
'获取一个可用的文件号
Dim MyFileNum As Integer = FreeFile()
'打开指定的文件,进行读取操作
FileOpen(MyFileNum, "C:\My.ini", OpenMode.Input)
Do While Not EOF(MyFileNum)
'读取一行
MyStr = LineInput(MyFileNum)
If MyStr "b=2" Then
If AllStr = "" Then
AllStr = AllStr MyStr
Else
AllStr = AllStr vbCrLf MyStr
End If
End If
Loop
FileClose(MyFileNum) '关闭文件
'写文件
Dim MyStream As New System.IO.FileStream("C:\My.ini", IO.FileMode.Create)
Dim MyWriter As New System.IO.StreamWriter(MyStream, System.Text.Encoding.UTF8)
MyWriter.WriteLine(AllStr)
MyWriter.Flush()
MyWriter.Close()
MyStream.Close()
End Sub
End Class
两种方法
1、API
[名称] 读写INI文件的四个函数
[数据来源] 自己制作
[来源时间] 2002.01.04
[内容简介] 空
[源代码内容]
'文件名SourceDB.ini文件
Private 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
Private 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
'以下两个函数,读/写ini文件,固定节点setting,in_key为写入/读取的主键
'仅仅针对是非值
'Y:yes,N:no,E:error
Public Function GetIniTF(ByVal In_Key As String) As Boolean
On Error GoTo GetIniTFErr
GetIniTF = True
Dim GetStr As String
GetStr = VBA.String(128, 0)
GetPrivateProfileString "Setting", In_Key, "", GetStr, 256, App.Path "\SourceDB.ini"
GetStr = VBA.Replace(GetStr, VBA.Chr(0), "")
If GetStr = "1" Then
GetIniTF = True
GetStr = ""
Else
GoTo GetIniTFErr
End If
Exit Function
GetIniTFErr:
Err.Clear
GetIniTF = False
GetStr = ""
End Function
Public Function WriteIniTF(ByVal In_Key As String, ByVal In_Data As Boolean) As Boolean
On Error GoTo WriteIniTFErr
WriteIniTF = True
If In_Data = True Then
WritePrivateProfileString "Setting", In_Key, "1", App.Path "\SourceDB.ini"
Else
WritePrivateProfileString "Setting", In_Key, "0", App.Path "\SourceDB.ini"
End If
Exit Function
WriteIniTFErr:
Err.Clear
WriteIniTF = False
End Function
'以下两个函数,读/写ini文件,不固定节点,in_key为写入/读取的主键
'针对字符串值
'空值表示出错
Public Function GetIniStr(ByVal AppName As String, ByVal In_Key As String) As String
On Error GoTo GetIniStrErr
If VBA.Trim(In_Key) = "" Then
GoTo GetIniStrErr
End If
Dim GetStr As String
GetStr = VBA.String(128, 0)
GetPrivateProfileString AppName, In_Key, "", GetStr, 256, App.Path "\SourceDB.ini"
GetStr = VBA.Replace(GetStr, VBA.Chr(0), "")
If GetStr = "" Then
GoTo GetIniStrErr
Else
GetIniStr = GetStr
GetStr = ""
End If
Exit Function
GetIniStrErr:
Err.Clear
GetIniStr = ""
GetStr = ""
End Function
Public Function WriteIniStr(ByVal AppName As String, ByVal In_Key As String, ByVal In_Data As String) As Boolean
On Error GoTo WriteIniStrErr
WriteIniStr = True
If VBA.Trim(In_Data) = "" Or VBA.Trim(In_Key) = "" Or VBA.Trim(AppName) = "" Then
GoTo WriteIniStrErr
Else
WritePrivateProfileString AppName, In_Key, In_Data, App.Path "\SourceDB.ini"
End If
Exit Function
WriteIniStrErr:
Err.Clear
WriteIniStr = False
End Function
以上代码保存于: SourceCode Explorer(源代码数据库)
复制时间: 2007.12.26 17:29:09
软件版本: 1.0.882
软件作者: Shawls
E-Mail: ShawFile@163.com
QQ: 9181729
2、用open打开文件全部读入,如果有修改覆盖写入文件
缓冲区是存放字符串的,防止字符串过大把内存溢出,引起异常。是中间的一个缓冲地带。这个是原理。你用那个函数就行了。
写入:Dim sr As New IO.StreamWriter(Application.StartupPath "/写入的文本.txt")
sr.WriteLine("写入的内容") sr.Close()读取:If (File.Exists(Application.StartupPath "/msg.txt")) Then
Dim fm As New IO.FileStream(Application.StartupPath "/读取的文本.txt", FileMode.Open)
Dim sr As IO.StreamReader = New IO.StreamReader(fm)
Do While sr.Peek() = 0
TextBox1.Text = sr.ReadLine() (读取文本到文本框)
Loop end if