ITPub博客

首页 > Linux操作系统 > Linux操作系统 > VB.NET 读写ini文件

VB.NET 读写ini文件

原创 Linux操作系统 作者:iDotNetSpace 时间:2009-03-09 14:11:31 0 删除 编辑

 Public Class Form1

    '************ini文件内容为*************************
    '*[send]                                           *                 
    '*send1=1                                          *
    '*send2=4                                          *
    '*send3=3                                          *
    '*IsSms=1                                          *
    '**************************************************

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim path As String
        'ini文件名为 Send.ini
        path = Application.StartupPath + "\Send.ini"
        TextBox1.Text = GetINI("Send", "Send1", "", path)
        TextBox2.Text = GetINI("Send", "Send2", "", path)
        Dim IsSms As Integer = GetINI("Send", "IsSms", "", path)
        If (IsSms = 1) Then
            Me.RadioButton1.Checked = True
        ElseIf (IsSms = 0) Then
            Me.RadioButton2.Checked = True
        End If
    End Sub

    '读取ini文件内容
    Public Function GetINI(ByVal Section As String, ByVal AppName As String, ByVal lpDefault As String, ByVal FileName As String) As String
        Dim Str As String = LSet(Str, 256)
        GetPrivateProfileString(Section, AppName, lpDefault, Str, Len(Str), FileName)
        Return Microsoft.VisualBasic.Left(Str, InStr(Str, Chr(0)) - 1)
    End Function

    '写ini文件操作
    Public Function WriteINI(ByVal Section As String, ByVal AppName As String, ByVal lpDefault As String, ByVal FileName As String) As Long
        WriteINI = WritePrivateProfileString(Section, AppName, lpDefault, FileName)
    End Function

    '读ini API函数
    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 Int32, ByVal lpFileName As String) As Int32
    '写ini API函数
    Private Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpString As String, ByVal lpFileName As String) As Int32


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Try
            Dim path As String
            path = Application.StartupPath + "\Send.ini"
            WriteINI("Send", "Send1", TextBox1.Text, path)
            WriteINI("Send", "Send2", TextBox2.Text, path)
            If (Me.RadioButton1.Checked = True) Then
                WriteINI("Send", "IsSms", 1, path)
            ElseIf (Me.RadioButton2.Checked = True) Then
                WriteINI("Send", "IsSms", 0, path)
            End If
            MsgBox("配置设置已经成功!!!!")
        Catch ex As Exception
            MsgBox("错误!!!!")
        End Try
    End Sub
End Class

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/12639172/viewspace-564905/,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2008-01-04

  • 博文量
    2376
  • 访问量
    5321231