明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 2954|回复: 3

读写INI文件的API函数及读写配置文件函数SetCfg和GetCfg

[复制链接]
发表于 2003-10-19 20:53:00 | 显示全部楼层 |阅读模式
本帖最后由 作者 于 2003-11-13 19:22:34 编辑

  1. '获取INI文件的整个Section段的值
  2. Declare Function GetPrivateProfileSection Lib "kernel32" _
  3.         Alias "GetPrivateProfileSectionA" _
  4.         (ByVal lpAppName As String, _
  5.         ByVal lpReturnedString As String, _
  6.         ByVal nSize As Long, _
  7.         ByVal lpFileName As String) As Long

  8. '获取INI文件某个Section段的某个Key的值
  9. Declare Function GetPrivateProfileString Lib "kernel32" _
  10.         Alias "GetPrivateProfileStringA" _
  11.         (ByVal lpApplicationName As String, _
  12.         ByVal lpKeyName As Any, _
  13.         ByVal lpDefault As String, _
  14.         ByVal lpReturnedString As String, _
  15.         ByVal nSize As Long, _
  16.         ByVal lpFileName As String) As Long

  17. '将一个格式化的Section段写入INI文件中
  18. Declare Function WritePrivateProfileSection Lib "kernel32" _
  19.         Alias "WritePrivateProfileSectionA" _
  20.         (ByVal lpAppName As String, _
  21.         ByVal lpString As Any, _
  22.         ByVal lpFileName As String) As Long

  23. '将一个字符串写入INI文件中的Section中的Key值
  24. Declare Function WritePrivateProfileString Lib "kernel32" _
  25.         Alias "WritePrivateProfileStringA" _
  26.         (ByVal lpApplicationName As String, _
  27.         ByVal lpKeyName As Any, _
  28.         ByVal lpString As Any, _
  29.         ByVal lpFileName As String) As Long

  30. '以下是把API函数转化成易懂的函数供大家使用:
  31. Public Function GetSection(IniFile As String, Section As String) As Variant
  32.     Dim sSection As String * 32767
  33.     Dim S As String
  34.    
  35.     GetPrivateProfileSection Section, sSection, Len(sSection), IniFile
  36.     S = sSection
  37.     S = Left(S, InStr(1, S, vbNullChar & vbNullChar) - 1)
  38.     S = Trim(S)
  39.     GetSection = Split(S, vbNullChar)
  40. End Function

  41. Public Function GetKey(IniFile As String, Section As String, Key As String, Default As String) As String
  42.     Dim Value As String * 32767
  43.     Dim S As String
  44.     GetPrivateProfileString Section, Key, Default, Value, Len(Value), IniFile
  45.     S = Value
  46.     S = Left(S, InStr(1, S, vbNullChar) - 1)
  47.     S = Trim(S)
  48.     GetKey = S
  49. End Function

  50. Public Sub SetSection(IniFile As String, Section As String, Value As Variant)
  51.     Dim i As Integer
  52.     Dim S As String
  53.     For i = LBound(Value) To UBound(Value)
  54.         If i = 0 Then
  55.             S = Value(i)
  56.         Else
  57.             S = S & vbNullChar & Value(i)
  58.         End If
  59.     Next i
  60.     S = S & vbNullChar & vbNullChar
  61.     S = Value
  62.     WritePrivateProfileSection Section, S, IniFile
  63. End Sub

  64. Public Sub SetKey(IniFile As String, Section As String, Key As String, Value As String)
  65.     WritePrivateProfileString Section, Key, Value, IniFile
  66. End Sub

 楼主| 发表于 2003-11-13 16:02:00 | 显示全部楼层
使用以上函数实现对系统配置文件acad2004.cfg的读取和修改,功能与LISP中的getcfg和setcfg完全相同

  1. Sub gc()
  2.     Debug.Print GetCfg("appdata/tyl/other")
  3. End Sub
  4. Sub sc()
  5.     Debug.Print SetCfg("appdata/tyl/other", "明经通道 http://www.mjtd.com")
  6. End Sub

  7. Function GetCfg(cfgname As String) As String
  8.     Dim strCfgFile As String
  9.     strCfgFile = ThisDrawing.Application.Preferences.Files.ConfigFile
  10.     Dim st As String
  11.     Dim CfgSection As String
  12.     Dim CfgKey As String
  13.     Dim Cfgvar As Variant
  14.     Cfgvar = Split(cfgname, "/")
  15.     CfgKey = Cfgvar(UBound(Cfgvar))
  16.     Dim i As Integer
  17.     For i = 0 To UBound(Cfgvar) - 1
  18.         CfgSection = CfgSection & "/" & Cfgvar(i)
  19.     Next
  20.     CfgSection = Right(CfgSection, Len(CfgSection) - 1)
  21.     GetCfg = GetKey(strCfgFile, CfgSection, CfgKey, "")
  22. End Function

  23. Function SetCfg(cfgname As String, cfgval As String) As String
  24.     Dim strCfgFile As String
  25.     strCfgFile = ThisDrawing.Application.Preferences.Files.ConfigFile
  26.     Dim st As String
  27.     Dim CfgSection As String
  28.     Dim CfgKey As String
  29.     Dim Cfgvar As Variant
  30.     Cfgvar = Split(cfgname, "/")
  31.     CfgKey = Cfgvar(UBound(Cfgvar))
  32.     Dim i As Integer
  33.     For i = 0 To UBound(Cfgvar) - 1
  34.         CfgSection = CfgSection & "/" & Cfgvar(i)
  35.     Next
  36.     CfgSection = Right(CfgSection, Len(CfgSection) - 1)
  37.     On Error Resume Next
  38.     SetKey strCfgFile, CfgSection, CfgKey, cfgval
  39.     If Not Err Then SetCfg = cfgval
  40. End Function
发表于 2003-11-29 21:10:00 | 显示全部楼层
今天才看到,用起来非常方便。和SaveSetting等命令差不多。
用setsection时还可以用数组直接建立若干个section。
打包, 谢谢明总。
发表于 2009-11-25 08:50:00 | 显示全部楼层
怎么看不到附件?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-11-26 00:44 , Processed in 0.182217 second(s), 23 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表