如何在VC中实现配置文件(ini)的读写

如何在VC中实现配置文件(ini)的读写,第1张

配置文件在重要性不言而喻,在我们常用的软件中经常可以看到它的身影,它提供了程序初始化过程中一些常用的参数,并且可以手动的修改这些参数,因此使用起来非常的方便。常见的配置文件为*.ini文件。[小节名]关键字=值关键字=值……MFC为用户读取ini文件提供了几个函数,其中常用的几个函数分别如下:读取信息:GetPrivateProfileString和GetPrivateProfileInt写入信息:WritePrivateProfileString运用这几个函数就可以满足常用的对字符串和整数的读写 *** 作了。为了体现MFC的封装性以及方便使用,我们可以定义一个接口,即一个纯虚类。所有的方法都由这个接口继承而来。我们将这个纯虚类命名为CCfgFile,之后我们从这个纯虚类中继承一个类(CIniFile)用来实现对ini文件的读取。以后若是需要一些更高级的方法可以再从CCfgFile继承出其他的类来实现。这样我们就可以利用CIniFile类中定义的函数来 *** 纵ini文件了。在程序中我们需要 *** 作ini文件中一些常用的配置参数读写,我们可以定义一个参数类来实现,如CParam这里需要注意的是在程序中我们可能在很多地方都要实现配置参数的读写,我们不能在每个要使用的地方都通过new关键字来创建一个CParam对象。原因你懂的,呵呵!那么我们可以通过定义CParam的一个静态成员来实现,这个静态成员通过一个静态的成员函数来获取。

打开INI文件的方法

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 '

其它由你自己补充了。


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/tougao/11496031.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-16
下一篇 2023-05-16

发表评论

登录后才能评论

评论列表(0条)

保存