VBGood网站全文搜索 Google

搜索VBGood全站网页(全文搜索)

VB爱好者乐园(VBGood)

 找回密码
 立即注册
搜索
查看: 6537|回复: 2

公布第九期C组擂主

[复制链接]
 楼主| 发表于 2006-6-9 09:45:33 | 显示全部楼层 |阅读模式
第九期C组只有ben1222参与,特宣布他为本期C组擂主!
下面是他的参赛作品,已能完成所有读取ini文件功能,值得保留。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
 楼主| 发表于 2006-6-9 09:48:05 | 显示全部楼层
同时发一个我常用的读取ini文件方法
新建一个类拷入以下内容即可。
Option Explicit
'*****************************************************
'本类用于读写ini文件
'*****************************************************

Public filename As String
Private success%

Private Declare Function WritePrivateProfileString Lib "KERNEL32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, 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 String, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long

Public Function readfile(fieldsetname As String, FieldName As String)
   If FileExist(filename) Then
      readfile = ReadWriteINI("GET", fieldsetname, FieldName)
  End If
End Function

Public Sub writefile(fieldsetname As String, FieldName As String, fieldvalue As String)
       success% = WritePrivateProfileString(fieldsetname, FieldName, fieldvalue, filename)
End Sub

Rem  向ini文件写内容;Mode 标志是读还是写;tmpSecname段落名称;tempKeyname项目名称;tempKeyValue项目值
Private Function ReadWriteINI(Mode As String, tmpSecname As String, tmpKeyname As String, Optional tmpKeyValue) As String
Dim tmpString As String '该变量保存过程的返回值

Dim secname As String   '段落名称
Dim KeyName As String   '项目名称
Dim keyvalue As String  '项目值
Dim anInt
Dim defaultkey As String

On Error GoTo ReadWriteINIError

If IsNull(Mode) Or Len(Mode) = 0 Then
  ReadWriteINI = "ERROR MODE"    ' 设置返回值
  Exit Function
End If
If IsNull(tmpSecname) Or Len(tmpSecname) = 0 Then
  ReadWriteINI = "ERROR Secname" ' 设置返回值
  Exit Function
End If
If IsNull(tmpKeyname) Or Len(tmpKeyname) = 0 Then
  ReadWriteINI = "ERROR Keyname" ' 设置返回值
  Exit Function
End If

' ******* '如果是写 *************************************
  If UCase(Mode) = "WRITE" Then
      If IsNull(tmpKeyValue) Or Len(tmpKeyValue) = 0 Then
        ReadWriteINI = "ERROR KeyValue"
        Exit Function
      Else
      Rem 将外部参数值传递给内部变量
      secname = tmpSecname
      KeyName = tmpKeyname
      keyvalue = tmpKeyValue
      anInt = WritePrivateProfileString(secname, KeyName, keyvalue, filename)
      End If
  End If

  ' *******  如果是读 *************************************
  If UCase(Mode) = "GET" Then
  
      secname = tmpSecname
      KeyName = tmpKeyname
      defaultkey = "Failed"
      keyvalue = String$(500, 32)
      anInt = GetPrivateProfileString(secname, KeyName, defaultkey, keyvalue, Len(keyvalue), filename)
      If Left(keyvalue, 6) <> "Failed" Then        ' *** got it
         tmpString = keyvalue
         tmpString = RTrim(tmpString)
         tmpString = Left(tmpString, Len(tmpString) - 1)
      End If
      ReadWriteINI = tmpString
  End If
  
Exit Function
ReadWriteINIError:
End Function

Private Function FileExist(Fname As String) As Boolean
    On Local Error Resume Next
   FileExist = (Dir(Fname) <> "")
End Function
回复 支持 反对

使用道具 举报

发表于 2013-3-30 23:54:55 | 显示全部楼层
一直都是他
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

文字版|手机版|小黑屋|VBGood  

GMT+8, 2022-7-3 16:50

VB爱好者乐园(VBGood)
快速回复 返回顶部 返回列表