如何将ini中连接字串读到模块中的常量中
我在global中定义了常量constr,原来是写死的,现在想从ini中读取,如何实现,主要是先后顺序,在模块中可以写读取操作的代码吗? 问题点数:50、回复次数:5Top
1 楼lihonggen0(李洪根,MS MVP,标准答案来了)回复于 2003-09-03 11:53:41 得分 0
建立与读取.ini文件
'请於form中放3个TextBox,一个CommandBox
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
Private Sub Command1_Click()
Dim success As Long
success = WritePrivateProfileString("MyApp", "text1", Text1.Text, "c:\aa.ini")
'叁数一 Section Name
'叁数二 於.ini中的项目
'叁数三 项目的内容
'叁数四 .ini文件的名称
success = WritePrivateProfileString("MyApp", "text2", Text2.Text, "c:\aa.ini")
success = WritePrivateProfileString("MyApp2", "text3", Text3.Text, "c:\aa.ini")
End Sub
Private Sub Form_load()
Dim ret As Long
Dim buff As String
buff = String(255, 0)
ret = GetPrivateProfileString("Myapp", "text1", "text1", buff, 256, "c:\aa.ini")
'若.ini MyApp中无text1,则采用叁数三的值
Text1.Text = buff
buff = String(255, 0)
ret = GetPrivateProfileString("Myapp", "text2", "text2", buff, 256, "c:\aa.ini")
Text2.Text = buff
buff = String(255, 0)
ret = GetPrivateProfileString("Myapp2", "text3", "text3", buff, 256, "c:\aa.ini")
Text3.Text = buff
End Sub
Top
2 楼tonylk(=www.tonixsoft.com=)回复于 2003-09-03 12:01:23 得分 10
VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
Persistable = 0 'NotPersistable
DataBindingBehavior = 0 'vbNone
DataSourceBehavior = 0 'vbNone
MTSTransactionMode = 0 'NotAnMTSObject
END
Attribute VB_Name = "clsInifile"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Option Explicit
'***************************************************************
'IniFile class
' 2003.06.04 tony
' lastmodified 2003.08.26
'***************************************************************
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
Private Declare Function GetPrivateProfileInt Lib "kernel32" Alias "GetPrivateProfileIntA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal nDefault As Long, ByVal lpFileName As String) As Long
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 mFileName As String
Public Sub SetFileName(AFileName As String)
mFileName = AFileName
End Sub
Public Sub SetString(ASection As String, AItem As String, AValue As String)
Call WritePrivateProfileString(ASection, AItem, AValue, mFileName)
End Sub
Public Sub Setlong(ASection As String, AItem As String, AValue As Long)
Dim ValueTmp As String
ValueTmp = Str(AValue)
Call WritePrivateProfileString(ASection, AItem, ValueTmp, mFileName)
End Sub
Public Sub SetBoolean(ASection As String, AItem As String, AValue As Boolean)
Dim IntVal As Long
If AValue Then
IntVal = 1
Else
IntVal = 0
End If
Call Setlong(ASection, AItem, IntVal)
End Sub
Public Function GetString(ASection As String, AItem As String, ADefault As String) As String
Dim Buff As String
Dim BuffSize As Long
BuffSize = 700
Buff = String(BuffSize, Chr(0))
Call GetPrivateProfileString(ASection, AItem, ADefault, Buff, BuffSize, mFileName)
BuffSize = InStr(Buff, Chr(0))
GetString = Trim(Left(Buff, BuffSize - 1))
End Function
Public Function Getlong(ASection As String, AItem As String, ADefault As Long) As Long
Getlong = GetPrivateProfileInt(ASection, AItem, ADefault, mFileName)
End Function
Public Function GetBoolean(ASection As String, AItem As String, ADefault As Boolean) As Boolean
Dim IntVal As Long
Dim RetVal As Long
If ADefault Then
IntVal = 1
Else
IntVal = 0
End If
RetVal = Getlong(ASection, AItem, IntVal)
If RetVal = 0 Then
GetBoolean = False
Else
GetBoolean = True
End If
End Function
Top
3 楼tonylk(=www.tonixsoft.com=)回复于 2003-09-03 12:01:59 得分 10
调用:
Private Sub LoadSettings()
Dim Ini As New clsInifile
On Error GoTo ErrHandler
Call Ini.SetFileName(AddSplash(App.Path) & ConfigFileName)
BusSpeed = Ini.Getlong("General", "BusSpeed", 50)
BusDelay1 = Ini.Getlong("General", "BusDelay1", 1)
BusDelay2 = Ini.Getlong("General", "BusDelay2", 1)
TicketFlashTime = Ini.Getlong("General", "TicketFlashTime", 1)
TicketDelayTime = Ini.Getlong("General", "TicketDelayTime", 60)
GlideSpeed = Ini.Getlong("General", "GlideSpeed", 50)
CommBaudRate = Ini.Getlong("Comm", "CommBaudRate", 115200)
Exit Sub
ErrHandler:
Resume Next
End Sub
Top
4 楼cuizm(射天狼 http://www.j2soft.cn/)回复于 2003-09-03 12:12:53 得分 10
Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) 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
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 Sub Form_Load()
Open "C:\aa.ini" For Output As #1
Print #1, "文件内容"
Close #1
End Sub
Private Sub Label1_Click()
On Error GoTo Errhandle
ShellExecute Me.hwnd, "open", "www.sohu.com", vbNullString, vbNullString, 1
Exit Sub
Errhandle:
MsgBox Err.Description & "!", vbInformation
End Sub
'写INI文件
Private Sub Command1_Click()
Dim Counter As Long
For Counter = 1 To 4
Call WriteToIni(App.Path & "\Options.ini", "Test", "Name" & Counter, "Value" & Counter)
Next Counter
End Sub
'读INI文件
Private Sub Command2_Click()
Dim Counter As Long
Dim Value(3) As String
For Counter = 1 To 4
Value(Counter - 1) = ReadFromIni(App.Path & "\Options.ini", "Test", "Name" & Counter)
Next Counter
End Sub
Top
5 楼m366(掩耳盗铃)回复于 2003-09-11 09:32:37 得分 20
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
Top




