1、由于INI文件本质上是文本文件,因此任何文本编辑器都能将其打开并进行编辑。不过,如果并没有设置文件类型关联,文本编辑器默认是不能打开INI文件的。
2、下面,我们就以打开著名系统维护工具ccleaner的配置文件ccleaner.ini为例,来教大家打开INI文件的方法。
3、要想打开INI文件,我们首先要做的是,在需要打开的INI文件上右击鼠标,然后选择“打开方式”-“选择默认程序”。
4、在此提醒大家注意,如果你不曾打开过或关联过INI文件,打开方式中是没有打开程序列表的。
5、然后在打开的菜单中点击“更多选项”。注意,如果你不曾打开过或关联过INI文件,此时的菜单中也可能是没有打开程序列表的。当然,不排除系统本身会推荐一些程序。
6、在扩展的程序列表中,如果仍没有出现文本编辑程序,或者你想用自己指定的文本编辑器打开INI文件,请点击“在这台电脑上查找其他应用”。
7、然后,找到一个文本编辑程序,打开。比如,我这里指定一个XML编辑器来打开它。
8、打开之后,我们就可以在其中编辑INI文件了。
下面是一个VB6的类的源代码,来自国外网站的一个技术专家,类名:CSetting,可以读写ini配置文件,或者读写注册表配置。Option Explicit
'Settings interface class
'Copyright ?2000 - Stan Schultes
'Written for VBPJ Getting Started September, 2000
'CSetting mode enum
Public Enum csModes
csModeRegistry = 0
csModeINI = 1
End Enum
'class member variables
Private m_eSaveMode As csModes
'module-level variables
Private msININame As String 'name of .INI file
Private msAppName As String 'app name for settings
'INI setting APIs
Private Declare Function GetPrivateProfileInt Lib "kernel32" Alias "GetPrivateProfileIntA" (ByVal SectionName As String, ByVal KeyName As String, ByVal Default As Long, ByVal FileName As String) As Long
Private Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal SectionName As String, ByVal KeyName As String, ByVal Default As String, ByVal ReturnedString As String, ByVal StringSize As Long, ByVal FileName As String) As Long
Private Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal SectionName As String, ByVal KeyName As String, ByVal KeyValue As String, ByVal FileName As String) As Long
'there is no WritePrivateProfileInt declaration...
'Error definitions
Private Const mklErrOffset As Long = vbObjectError + 512
Private Const mklErrNotNumeric As Long = mklErrOffset + 1
Private Const mksErrNotNumeric As String = "The setting value returned was not numeric"
Private Const mklErrWriting As Long = mklErrOffset + 2
Private Const mksErrWriting As String = "Error writing Setting "
Private Const mklErrReading As Long = mklErrOffset + 3
Private Const mksErrReading As String = "Error reading Setting "
Public Function Init(Optional ByVal AppPath As String, Optional ByVal AppName As Variant, Optional ByVal Mode As csModes = csModeRegistry)
'Set up class variables, default to Registry mode
If IsMissing(AppPath) Then
msININame = App.Path
Else
msININame = Trim$(AppPath)
End If
If IsMissing(AppName) Then
msAppName = App.EXEName
Else
msAppName = Trim$(AppName)
End If
'default to the app's .exe path
msININame = msININame &"\" &msAppName &".ini"
'msININame = App.Path &"\" &msAppName &".ini"
SaveMode = Mode
End Function
Public Function GetSettingStr(ByVal Section As String, ByVal KeyName As String, ByVal DefaultValue As String) As String
'returns a string setting
Dim lRet As Long
Dim sBuf As String * 128
On Error GoTo GetSettingStr_Error
If Len(msAppName) = 0 Then Init
Select Case m_eSaveMode
Case csModeRegistry
GetSettingStr = GetSetting(msAppName, Section, KeyName, DefaultValue)
Case Else
lRet = GetPrivateProfileString(Section, KeyName, DefaultValue, sBuf, Len(sBuf), msININame)
GetSettingStr = TrimNull(sBuf)
End Select
GetSettingStr_Exit:
Exit Function
GetSettingStr_Error:
Err.Raise mklErrReading, "CSetting.GetSettingStr", mksErrReading &"(" &Err &", " &Error &")"
End Function
Public Function GetSettingInt(ByVal Section As String, ByVal KeyName As String, ByVal DefaultValue As Integer) As Integer
'returns a numeric setting
Dim sSetting As String
On Error GoTo GetSettingInt_Error
If Len(msAppName) = 0 Then Init
Select Case m_eSaveMode
Case csModeRegistry
sSetting = GetSetting(msAppName, Section, KeyName, CStr(DefaultValue))
'check if value is numeric
If IsNumeric(sSetting) Then
GetSettingInt = CInt(sSetting)
Else
'match behavior of .INI return
GetSettingInt = 0
'or - return error if desired
'On Error GoTo 0 'disable handler to return error
'Err.Raise mklErrNotNumeric, "CSetting.GetSettingInt", mksErrNotNumeric
End If
Case Else
'returns 0 if not numeric
GetSettingInt = GetPrivateProfileInt(Section, KeyName, DefaultValue, msININame)
End Select
GetSettingInt_Exit:
Exit Function
GetSettingInt_Error:
Err.Raise mklErrReading, "CSetting.GetSettingInt", mksErrReading &"(" &Err &", " &Error &")"
End Function
Public Sub SaveSettingStr(ByVal Section As String, ByVal KeyName As String, ByVal Setting As String)
'saves a string setting
Dim lRet As Long
On Error GoTo SaveSettingStr_Error
If Len(msAppName) = 0 Then Init
Select Case m_eSaveMode
Case csModeRegistry
SaveSetting msAppName, Section, KeyName, Setting
Case Else
lRet = WritePrivateProfileString(Section, KeyName, Setting, msININame)
End Select
SaveSettingStr_Exit:
Exit Sub
SaveSettingStr_Error:
Err.Raise mklErrWriting, "CSetting.SaveSettingStr", mksErrWriting &"(" &Err &", " &Error &")"
End Sub
Public Sub SaveSettingInt(ByVal Section As String, ByVal KeyName As String, Setting As Integer)
'saves a numeric setting
Dim lRet As Long
On Error GoTo SaveSettingInt_Error
If Len(msAppName) = 0 Then Init
Select Case m_eSaveMode
Case csModeRegistry
SaveSetting msAppName, Section, KeyName, CStr(Setting)
Case Else
lRet = WritePrivateProfileString(Section, KeyName, CStr(Setting), msININame)
End Select
SaveSettingInt_Exit:
Exit Sub
SaveSettingInt_Error:
Err.Raise mklErrWriting, "CSetting.SaveSettingInt", mksErrWriting &"(" &Err &", " &Error &")"
End Sub
Public Property Let SaveMode(Mode As csModes)
'sets save mode, see csModes enum
m_eSaveMode = Mode
End Property
Public Property Get SaveMode() As Long
'returns save mode
SaveMode = CLng(m_eSaveMode)
End Property
Private Function TrimNull(ByVal InString As String) As String
'trims string at first Null character
Dim lPos As Long
TrimNull = Trim$(InString)
lPos = InStr(TrimNull, vbNullChar)
If lPos >0 Then TrimNull = Left$(TrimNull, lPos - 1)
End Function
Private Sub Class_Initialize()
msININame = App.Path &"\" &App.EXEName &".ini"
End Sub
使用方法:
在程序的全局模块中定义CSetting的类实例变量:(以下定义了两个,分别读写ini和注册表)
Public SetIniAs CSetting '读写ini配置文件参数 '
Public SetRegAs CSetting'读写注册表配置参数
在sub main 中初始化:(其中PATH_EXE是你的程序路径,放ini文件的路径)
Set SetIni = New CSetting
SetIni.Init PATH_Exe, App.Title, csModeINI
Set SetReg = New CSetting
SetReg.Init PATH_Exe, App.Title, csModeRegistry
这就可以随便用了。
dbPassword = SetIni.GetSettingStr(NameDB, "Password", "111111")
dbUserID = SetIni.GetSettingStr(NameDB, "UserID", "123456")
dbCatalog = SetIni.GetSettingStr(NameDB, "Catalog", "aaa")
dbDataSource = SetIni.GetSettingStr(NameDB, "DataSource", sky)
cnStr= "Provider=SQLOLEDB.1Password=" &dbPassword &"Persist Security Info=TrueUser ID=" &dbUserID &"Initial Catalog=" & dbCatalog &"Data Source=" &dbDataSource '
其它由你自己补充了。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)