zoukankan      html  css  js  c++  java
  • 用vb.net读取INI配置文件的方法,使用API

    因为对XML前不了解,所以对XML方式来做配置文件我都不能很好的实现
    但为了应行,只有先使用INI的文来记录了
    也就沿用了VB6里的INI文读取方法

     '声明INI配置文件读写API函数
        Private Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As StringByVal lpKeyName As StringByVal lpDefault As StringByVal lpReturnedString As StringByVal nSize As Int32, ByVal lpFileName As StringAs Int32
        
    Private Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As StringByVal lpKeyName As StringByVal lpString As StringByVal lpFileName As StringAs Int32
        
    '定义读取配置文件函数
        Public Function GetINI(ByVal Section As StringByVal AppName As StringByVal lpDefault As StringByVal FileName As StringAs String
            
    Dim Str As String = LSet(Str256)
            GetPrivateProfileString(Section, AppName, lpDefault, 
    StrLen(Str), FileName)
            
    Return Microsoft.VisualBasic.Left(StrInStr(StrChr(0)) - 1)
        
    End Function

        
    '定义写入配置文件函数
        Public Function WriteINI(ByVal Section As StringByVal AppName As StringByVal lpDefault As StringByVal FileName As StringAs Long
            WriteINI 
    = WritePrivateProfileString(Section, AppName, lpDefault, FileName)
        
    End Function

        
    Private Sub Form1_Load(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles MyBase.Load
            
    Dim path As String
            path 
    = Application.StartupPath + "\server.ini"
            TextBox1.Text = GetINI("Server""IP""", path)
            TextBox2.Text 
    = GetINI("Server""port""", path)
        
    End Sub


        
    Private Sub Button1_Click(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles Button1.Click
            
    Try
                
    Dim path As String
                path 
    = Application.StartupPath + "\server.ini"
                WriteINI("Server""IP", TextBox1.Text, path)
                WriteINI(
    "Server""port", TextBox2.Text, path)
                
    MsgBox("配置设置已经成功!!!!")
                Me.Close()
            
    Catch ex As Exception
                
    MsgBox("错误!!!!")
            
    End Try

        
    End Sub
  • 相关阅读:
    RobotFrameWork(一)robotfamework(python版)及Ride在windows安装
    Sql日期时间格式转换[zhuan]
    SQL query
    WPF窗体视图中绑定Resources文件中字符串时,抛出:System.Windows.Markup.StaticExtension
    Power Map 更新日志
    球面墨卡托(Spherical Mercator)
    TPL(Task Parallel Library)多线程、并发功能
    WPF:保存窗口当前状态截图方法
    dynamic关键字
    Error: Cannot Determine the location of the VS common tools folder
  • 原文地址:https://www.cnblogs.com/aowind/p/102723.html
Copyright © 2011-2022 走看看