转至繁体中文版     | 网站首页 | 图文教程 | 资源下载 | 站长博客 | 图片素材 | 武汉seo | 武汉网站优化 | 
最新公告:     敏韬网|教学资源学习资料永久免费分享站!  [mintao  2008年9月2日]        
您现在的位置: 学习笔记 >> 图文教程 >> 软件开发 >> VB.NET程序 >> 正文
一个绝对经典的在VB中操作.ini文件的通用类源代码         ★★★★

一个绝对经典的在VB中操作.ini文件的通用类源代码

作者:闵涛 文章来源:闵涛的学习笔记 点击数:764 更新时间:2009/4/23 18:59:19

一个绝对经典的在VB中操作.ini文件的通用类源代码

(有需要源程序的,请在留言中留下电子邮件地址)

程序界面:

程序界面

源程序:

classIniFile.cls的内容:

Option Explicit

''''--------classIniFile.cls  代码----------------
''''这里定义了一个classIniFile类
''''一个绝对经典的在VB中操作.ini文件的通用类源代码
''''程序编写:中国青岛·许家国
''''    2002.6.16
''''E-Mail: goj2000@163.com
''''HomePage: http://www.gojclub.com
''''

''''Private  member  that  holds  a  reference  to
''''the  path  of  our  ini  file

Private strINI As String

''''Windows  API  Declares
Private Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" _
    (ByVal lpApplicationName As String, _
    ByVal lpKeyName As Any, _
    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 Any, _
    ByVal lpDefault As String, _
    ByVal lpReturnedString As String, _
    ByVal nSize As Long, _
    ByVal lpFileName As String) As Long

Private Function MakePath(ByVal strDrv As String, ByVal strDir As String) As String

    ''''  Makes  an  INI  file:  Guarantees  a  sub  dir
    Do While Right$(strDrv, 1) = "\"
          strDrv = Left$(strDrv, Len(strDrv) - 1)
    Loop
   
    Do While Left$(strDir, 1) = "\"
          strDir = Mid$(strDir, 2)
    Loop
   
    ''''  Return  the  path
    MakePath = strDrv & "\" & strDir
End Function

Private Sub CreateIni(strDrv As String, strDir As String)
    ''''  Make  a  new  ini  file
    strINI = MakePath(strDrv, strDir)
End Sub

Public Sub WriteIniKey(strSection As String, strKey As String, strValue As String)
    ''''  Write  to  strINI
    WritePrivateProfileString strSection, strKey, strValue, strINI
End Sub

Public Function GetIniKey(strSection As String, strKey As String) As String
    Dim strTmp As String
    Dim lngRet As String
    Dim I As Integer
    Dim strTmp2 As String
   
    strTmp = String$(1024, Chr(32))
    lngRet = GetPrivateProfileString(strSection, strKey, "", strTmp, Len(strTmp), strINI)
    strTmp = Trim(strTmp)
    strTmp2 = ""
    For I = 1 To Len(strTmp)
        If Asc(Mid(strTmp, I, 1)) <> 0 Then
            strTmp2 = strTmp2 + Mid(strTmp, I, 1)
        End If
    Next I
    strTmp = strTmp2
   
    GetIniKey = strTmp
End Function

Public Property Let INIFileName(ByVal New_IniPath As String)
    ''''  Sets  the  new  ini  path
    strINI = New_IniPath
End Property

Public Property Get INIFileName() As String
    ''''  Returns  the  current  ini  path
    INIFileName = strINI
End Property

''''***************************************清除KeyWord"键"(Sub)***********************************************
Public Function DelIniKey(ByVal SectionName As String, ByVal KeyWord As String)
    Dim RetVal As Integer
    RetVal = WritePrivateProfileString(SectionName, KeyWord, 0&, strINI)
End Function

''''如果是清除section就少写一个Key多一个""。
''''**************************************清除 Section"段"(Sub)***********************************************
Public Function DelIniSec(ByVal SectionName As String)      ''''清除section
    Dim RetVal As Integer
    RetVal = WritePrivateProfileString(SectionName, 0&, "", strINI)
End Function

 

Form1中的内容:

Option Explicit

''''一个绝对经典的在VB中操作.ini文件的通用类源代码示例程序
''''程序编写:中国青岛·许家国
''''    2002.6.16
''''E-Mail: goj2000@163.com
''''HomePage: http://www.gojclub.com

''''定义一个.ini类型的变量
Dim DemoIni As New classIniFile

Private Sub Form_Load()
    ''''对控件进行初始化
    Text1.Text = "测试一下"
    List1.Clear
   
    ''''定义.ini文件名,并写入一些初始数据
    DemoIni.INIFileName = App.Path & "\demoini.ini"
    DemoIni.WriteIniKey "系统", "启动路径", App.Path
    DemoIni.WriteIniKey "系统", "可执行程序文件名", App.EXEName
   
    ''''显示保存到.ini文件中的数据
    Call CmdRead_Click
End Sub

''''退出程序
Private Sub CmdExit_Click()
    Unload Me
End Sub

''''读取.ini文件中已经存在的数据并显示出来
Private Sub CmdRead_Click()
    Dim TestStr As String
   
    List1.Clear
    TestStr = DemoIni.GetIniKey("系统", "启动路径")
    List1.AddItem "系统 - 启动路径: " & TestStr
    TestStr = DemoIni.GetIniKey("系统", "可执行程序文件名")
    List1.AddItem "系统 - 可执行程序文件名: " & TestStr
    TestStr = DemoIni.GetIniKey("用户自定义", "用户数据")
    List1.AddItem "用户自定义 - 用户数据: " & TestStr
End Sub

''''保存用户自定义数据到.ini文件中
Private Sub CmdSave_Click()
    DemoIni.WriteIniKey "用户自定义", "用户数据", Text1.Text
   
    ''''显示保存到.ini文件中的数据
    Call CmdRead_Click
End Sub

''''清除用户自定义段和段中数据
Private Sub CmdDelete_Click()
    DemoIni.DelIniKey "用户自定义", "用户数据"
    DemoIni.DelIniSec "用户自定义"
   
    ''''显示保存到.ini文件中的数据
    Call CmdRead_Click
End Sub

 

 


没有相关教程
教程录入:mintao    责任编辑:mintao 
  • 上一篇教程:

  • 下一篇教程:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
      注:本站部分文章源于互联网,版权归原作者所有!如有侵权,请原作者与本站联系,本站将立即删除! 本站文章除特别注明外均可转载,但需注明出处! [MinTao学以致用网]
      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)

    同类栏目
    · C语言系列  · VB.NET程序
    · JAVA开发  · Delphi程序
    · 脚本语言
    更多内容
    热门推荐 更多内容
  • 没有教程
  • 赞助链接
    更多内容
    闵涛博文 更多关于武汉SEO的内容
    500 - 内部服务器错误。

    500 - 内部服务器错误。

    您查找的资源存在问题,因而无法显示。

    | 设为首页 |加入收藏 | 联系站长 | 友情链接 | 版权申明 | 广告服务
    MinTao学以致用网

    Copyright @ 2007-2012 敏韬网(敏而好学,文韬武略--MinTao.Net)(学习笔记) Inc All Rights Reserved.
    闵涛 投放广告、内容合作请Q我! E_mail:admin@mintao.net(欢迎提供学习资源)

    站长:MinTao ICP备案号:鄂ICP备11006601号-18

    闵涛站盟:医药大全-武穴网A打造BCD……
    咸宁网络警察报警平台