vb.netini读写 vbnet with

vb.net读写ini文件问题

以下是我的办法:OK!

成都创新互联主要从事成都网站建设、成都网站制作、网页设计、企业做网站、公司建网站等业务。立足成都服务庆云,10年网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务:028-86922220

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.net2005中到底怎么读取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 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 GetINI(ByVal Section As String, ByVal AppName As String, ByVal lpDefault As String, ByVal FileName As String) As String

Dim Str As String = LSet(Str, 256)

GetPrivateProfileString(Section, AppName, lpDefault, Str, Len(Str), FileName)

Return Microsoft.VisualBasic.Left(Str, InStr(Str, Chr(0)) - 1)

End Function

'写

Public Function WriteINI(ByVal Section As String, ByVal AppName As String, ByVal WriteString As String, ByVal FileName As String) As Long

WriteINI = WritePrivateProfileString(Section, AppName, WriteString, FileName)

End Function

其中,Section是像[ip]那样的(填里面的就可以了),AppName是“=”前面的项名称,lpDefault是如果没有可用的值,就返回lpDefault,FileName是文件名,WriteString是要写入的值

你所希望的:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

MsgBox(GetINI("ip", "user_id", "", "D:\DataBase.ini"))

'返回sa

End Sub

【vb.net】求个用VS2013读写INI的代码

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函数

‘说明同上。

vb读写ini文件

两种方法

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打开文件全部读入,如果有修改覆盖写入文件


分享题目:vb.netini读写 vbnet with
分享路径:http://ybzwz.com/article/hhppch.html