Cls_Ini.cls:VB写的操作ini配置文件的类

前端之家收集整理的这篇文章主要介绍了Cls_Ini.cls:VB写的操作ini配置文件的类前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

点击此处下载Cls_Ini.cls

主要源程序:

  1. VERSION 1.0 CLASS
  2. BEGIN
  3. MultiUse = -1 'True
  4. Persistable = 0 'NotPersistable
  5. DataBindingBehavior = 0 'vbNone
  6. DataSourceBehavior = 0 'vbNone
  7. MTSTransactionMode = 0 'NotAnMTSObject
  8. END
  9. Attribute VB_Name = "Cls_Ini"
  10. Attribute VB_GlobalNameSpace = False
  11. Attribute VB_Creatable = True
  12. Attribute VB_PredeclaredId = False
  13. Attribute VB_Exposed = False
  14. 'EXAMPLE
  15. 'ini.Path = "LampKensaki.ini"
  16. 'ini.Read
  17. 'strT10ComPortNo = ini.GetValue("T-10_COM_NO","T-10_Com_No") 'T-10_Com_No=6
  18. 'intT10WaitTime = ini.GetValue("T-10_WAIT_TIME","T10WaitTime") 'T10WaitTime=3000
  19. 'intShoudoSokuteiTimming = ini.GetValue("SHOUDO_SOKUTEI_TIMMING","ShoudoSokuteiTimming") 'ShoudoSokuteiTimming=15
  20. 'Ad_Ch = ini.GetValue("AD_CH","Ad_Ch")
  21.  
  22.  
  23. 'all the functions and variables in this file are used to operate(read/write) the .ini file
  24. Option Explicit
  25. Option Compare Binary
  26.  
  27. Private Const SP As String = "["
  28. Private Const EP As String = "]"
  29. Private Const CP As String = "="
  30. Private Const CB As String = ";"
  31.  
  32. Private Type INISection
  33. Name As String
  34. Keys() As String
  35. KeysCnt As Long
  36. End Type
  37.  
  38.  
  39.  
  40. Private INI_Path As String
  41. Private INI_Mode As Boolean
  42.  
  43. Private INI_SectCnt As Long
  44.  
  45. Private INI_Sect() As INISection
  46. Private INI_File() As String
  47.  
  48.  
  49.  
  50. Private Function CheckSect(ByVal Sect As String) As Boolean
  51. If Left$(Sect,1) = SP And Right$(Sect,1) = EP Then
  52. CheckSect = 1
  53. End If
  54. End Function
  55.  
  56. Private Function CheckKey(ByVal Key As String) As Boolean
  57. If Left$(Key,1) <> CB Then
  58. If InStr(Key,CP) Then
  59. CheckKey = 1
  60. End If
  61. End If
  62. End Function
  63.  
  64. Private Function GetSectName(ByVal Sect As String) As String
  65. GetSectName = Mid$(Sect,2,Len(Sect) - 2)
  66. End Function
  67.  
  68. Private Function GetKeyName(ByVal Key As String) As String
  69. Dim i As Long
  70.  
  71. i = InStr(Key,CP)
  72. If i > 0 Then
  73. GetKeyName = Left$(Key,i - 1)
  74. End If
  75. End Function
  76.  
  77. Private Function GetValueName(ByVal Key As String) As String
  78. Dim i As Long
  79.  
  80. i = InStr(Key,CP)
  81. If i > 0 Then
  82. GetValueName = Right$(Key,Len(Key) - i)
  83. End If
  84. End Function
  85.  
  86. Private Function ChangeSect(ByVal Sect As String) As String
  87. ChangeSect = SP & Sect & EP
  88. End Function
  89.  
  90. Private Function ChangeKey(ByVal Key As String,ByVal Value As String) As String
  91. ChangeKey = Key & CP & Value
  92. End Function
  93.  
  94. Private Sub StrectToAry()
  95. Dim File() As String
  96. Dim i As Long,t As Long,w As Long
  97. Dim s As Long
  98. Const Dumy As String = SP & "Dummy" & EP
  99.  
  100. On Error Resume Next
  101.  
  102. If INI_SectCnt <= 0 Then
  103. ReDim INI_File(0) As String
  104. Exit Sub
  105. End If
  106.  
  107. If INI_File(0) = "" Then
  108. If Err.Number Then
  109. ReDim INI_File(0) As String
  110. INI_File(0) = Dumy
  111. ' Err.Clear
  112. End If
  113. End If
  114.  
  115. Do
  116. If s >= INI_SectCnt Then Exit Do
  117. If CheckSect(INI_File(w)) Or w > UBound(INI_File) Then
  118. ReDim Preserve File(i) As String
  119. If s > 0 Then
  120. If File(i - 1) <> "" Then
  121. File(i) = ""
  122. i = i + 1
  123. ReDim Preserve File(i) As String
  124. End If
  125. End If
  126. File(i) = INI_Sect(s).Name
  127. For t = 0 To INI_Sect(s).KeysCnt - 1
  128. i = i + 1
  129. ReDim Preserve File(i) As String
  130. File(i) = INI_Sect(s).Keys(t)
  131. Next
  132. s = s + 1
  133. i = i + 1
  134. ElseIf Not CheckKey(INI_File(w)) Then
  135. ReDim Preserve File(i) As String
  136. File(i) = INI_File(w)
  137. i = i + 1
  138. End If
  139. w = w + 1
  140. Loop
  141.  
  142. ReDim INI_File(i - 1) As String
  143. INI_File = File
  144.  
  145. Err.Clear
  146. End Sub
  147.  
  148.  
  149.  
  150. Public Property Get Path() As String
  151. Path = INI_Path
  152. End Property
  153.  
  154. Public Property Let Path(ByVal nv As String)
  155. INI_Path = nv
  156. End Property
  157.  
  158. Public Property Get Mode() As Boolean
  159. Mode = INI_Mode
  160. End Property
  161.  
  162. Public Property Let Mode(ByVal nv As Boolean)
  163. INI_Mode = nv
  164. End Property
  165.  
  166. Public Property Get SectCount() As Long
  167. SectCount = INI_SectCnt
  168. End Property
  169.  
  170. Public Property Get KeyCount(Optional ByVal SectionNo As Long = -1,_
  171. Optional ByVal SectionName As String) As Long
  172. Dim i As Long
  173.  
  174. If INI_SectCnt <= SectionNo Then Exit Property
  175. If INI_SectCnt < 1 Then Exit Property
  176.  
  177. If SectionNo >= 0 Then
  178. KeyCount = INI_Sect(SectionNo).KeysCnt
  179. ElseIf SectionName <> "" Then
  180. For i = 0 To (INI_SectCnt - 1)
  181. If GetSectName(INI_Sect(i).Name) = SectionName Then
  182. KeyCount = INI_Sect(i).KeysCnt
  183. Exit For
  184. End If
  185. Next
  186. Else
  187. For i = 0 To (INI_SectCnt - 1)
  188. KeyCount = KeyCount + INI_Sect(i).KeysCnt
  189. Next
  190. End If
  191.  
  192. End Property
  193.  
  194. Public Property Get GetSect(ByVal SectionNo As Long) As String
  195.  
  196. If SectionNo < 0 Then Exit Property
  197. If INI_SectCnt > SectionNo Then
  198. GetSect = GetSectName(INI_Sect(SectionNo).Name)
  199. End If
  200.  
  201. End Property
  202.  
  203. Public Property Get GetKey(ByVal SectionName As String,ByVal KeyNo As Long) As String
  204. Dim i As Long
  205.  
  206. If INI_SectCnt < 1 Then Exit Property
  207. If KeyNo < 0 Then Exit Property
  208.  
  209. For i = 0 To (INI_SectCnt - 1)
  210. With INI_Sect(i)
  211. If GetSectName(.Name) = SectionName Then
  212. If .KeysCnt > KeyNo Then
  213. GetKey = GetKeyName(.Keys(KeyNo))
  214. End If
  215. Exit For
  216. End If
  217. End With
  218. Next
  219.  
  220. End Property
  221.  
  222. Public Property Get GetValue(ByVal SectionName As String,ByVal KeyName As String,_
  223. Optional ByVal Default As String = "") As String
  224. Dim i As Long,t As Long
  225.  
  226. GetValue = Default
  227.  
  228. If INI_SectCnt < 1 Then
  229. Exit Property
  230. End If
  231.  
  232. For i = 0 To (INI_SectCnt - 1)
  233. With INI_Sect(i)
  234. If GetSectName(.Name) = SectionName Then
  235. For t = 0 To (.KeysCnt - 1)
  236. If GetKeyName(.Keys(t)) = KeyName Then
  237. GetValue = GetValueName(.Keys(t))
  238. Exit Property
  239. End If
  240. Next
  241. End If
  242. End With
  243. Next
  244.  
  245. End Property
  246.  
  247. Public Function SetValue(ByVal SectionName As String,_
  248. ByVal Value As String) As Boolean
  249. Dim File() As String
  250. Dim i As Long,w As Long,z As Long
  251.  
  252.  
  253. If INI_Path = "" Then Exit Function
  254.  
  255. If INI_SectCnt > 0 Then
  256. For i = 0 To (INI_SectCnt - 1)
  257. If GetSectName(INI_Sect(i).Name) = SectionName Then
  258. If INI_Sect(i).KeysCnt > 0 Then
  259. For t = 0 To (INI_Sect(i).KeysCnt - 1)
  260. If GetKeyName(INI_Sect(i).Keys(t)) = KeyName Then
  261. INI_Sect(i).Keys(t) = ChangeKey(KeyName,Value)
  262. Exit For
  263. End If
  264. Next
  265. If INI_Sect(i).KeysCnt < 1 Or t > (INI_Sect(i).KeysCnt - 1) Then
  266. With INI_Sect(i)
  267. ReDim Preserve .Keys(t) As String
  268. .Keys(t) = ChangeKey(KeyName,Value)
  269. .KeysCnt = .KeysCnt + 1
  270. End With
  271. End If
  272. SetValue = 1
  273. Exit For
  274. End If
  275. End If
  276. Next
  277. End If
  278.  
  279. If INI_SectCnt < 1 Or i > (INI_SectCnt - 1) Then
  280. If INI_SectCnt < 1 Then i = 0
  281. ReDim Preserve INI_Sect(i) As INISection
  282. With INI_Sect(i)
  283. ReDim .Keys(0) As String
  284. .Name = ChangeSect(SectionName)
  285. .Keys(0) = ChangeKey(KeyName,Value)
  286. .KeysCnt = 1
  287. End With
  288. INI_SectCnt = INI_SectCnt + 1
  289. SetValue = 1
  290. End If
  291.  
  292.  
  293. If INI_Mode Then
  294. SetValue = Save
  295. End If
  296.  
  297. End Function
  298.  
  299. Public Function Delete(ByVal Section As String,Optional ByVal Key As String) As Boolean
  300. Dim iniSect() As INISection
  301. Dim iniKey() As String
  302. Dim i As Long,w As Long
  303.  
  304. If INI_SectCnt <= 0 Then
  305. Exit Function
  306. End If
  307.  
  308. If Key = "" Then
  309. For t = 0 To INI_SectCnt - 1
  310. If GetSectName(INI_Sect(t).Name) <> Section Then
  311. ReDim Preserve iniSect(w) As INISection
  312. iniSect(w) = INI_Sect(t)
  313. w = w + 1
  314. End If
  315. Next
  316. If w < t Then
  317. ReDim INI_Sect(w - 1) As INISection
  318. INI_Sect = iniSect
  319. INI_SectCnt = w
  320. Delete = 1
  321. End If
  322. Else
  323. For i = 0 To INI_SectCnt - 1
  324. If GetSectName(INI_Sect(i).Name) = Section Then
  325. If INI_Sect(i).KeysCnt <= 0 Then
  326. Exit For
  327. End If
  328. For t = 0 To INI_Sect(i).KeysCnt - 1
  329. If GetKeyName(INI_Sect(i).Keys(t)) <> Key Then
  330. ReDim Preserve iniKey(t) As String
  331. iniKey(w) = INI_Sect(i).Keys(t)
  332. w = w + 1
  333. End If
  334. Next
  335. If w < t Then
  336. ReDim INI_Sect(i).Keys(t - 1) As String
  337. INI_Sect(i).Keys = iniKey
  338. INI_Sect(i).KeysCnt = w
  339. Delete = 1
  340. End If
  341. End If
  342. Next
  343. End If
  344.  
  345. If INI_Mode Then
  346. Delete = Save
  347. End If
  348. End Function
  349.  
  350. Public Function Read() As Boolean
  351. Dim rl As String
  352. Dim NO As Integer
  353. Dim i As Long,w As Long
  354.  
  355. On Error Resume Next
  356.  
  357. NO = FreeFile()
  358. Open INI_Path For Input As #NO
  359. If Err.Number Or LOF(NO) = 0 Then
  360. Close #NO
  361. Exit Function
  362. End If
  363. Do While Not EOF(NO)
  364. ReDim Preserve INI_File(i) As String
  365. Line Input #NO,INI_File(i)
  366. i = i + 1
  367. Loop
  368. Close #NO
  369.  
  370. INI_SectCnt = -1
  371. For t = LBound(INI_File) To UBound(INI_File)
  372. If CheckSect(INI_File(t)) Then
  373. w = 0
  374. INI_SectCnt = INI_SectCnt + 1
  375. ReDim Preserve INI_Sect(INI_SectCnt) As INISection
  376. INI_Sect(INI_SectCnt).Name = INI_File(t)
  377. ElseIf CheckKey(INI_File(t)) Then
  378. ReDim Preserve INI_Sect(INI_SectCnt).Keys(w) As String
  379. INI_Sect(INI_SectCnt).Keys(w) = INI_File(t)
  380. INI_Sect(INI_SectCnt).KeysCnt = INI_Sect(INI_SectCnt).KeysCnt + 1
  381. w = w + 1
  382. End If
  383. Next
  384.  
  385. INI_SectCnt = INI_SectCnt + 1
  386. Read = 1
  387. End Function
  388.  
  389. Public Function Save() As Boolean
  390. Dim NO As Integer
  391. Dim i As Long
  392.  
  393. On Error Resume Next
  394.  
  395. Call StrectToAry
  396.  
  397. NO = FreeFile()
  398. Open INI_Path For Output As #NO
  399. If Err.Number Then
  400. Close #NO
  401. Exit Function
  402. End If
  403. For i = LBound(INI_File) To UBound(INI_File)
  404. If Err.Number Then
  405. Exit For
  406. End If
  407. Print #NO,INI_File(i)
  408. Next
  409. Close #NO
  410.  
  411. Save = 1
  412. End Function
  413.  
  414. Public Sub Release()
  415.  
  416. INI_Path = ""
  417. INI_Mode = 0
  418.  
  419. INI_SectCnt = 0
  420.  
  421. Erase INI_File
  422. Erase INI_Sect
  423.  
  424. End Sub

【更多阅读】

  1. [原]WMICodeCreator:C#产生WMI代码的工具
  2. [原]Cls_Ini.cls:VB写的操作ini配置文件的类
  3. [原]GetIcons:C#提取应用程序的图标资源
  4. [原]Baidu:C#利用百度来搜索网页、图片、视频等等
  5. [原]ManageStartUpApps:C#操作注册表来读取和修改开机启动项
  6. [原]Baidu:C#利用百度来搜索网页、图片、视频等等
  7. [译]用C#检测你的打印机是否连接
  8. [原]WMICodeCreator:C#产生WMI代码的工具
  9. [原]使用Excel的VBA来读取和修改bmp位图像素数据
  10. [原]DownloadWebImages:下载某页面上的所有图片

猜你在找的VB相关文章