2020年5月7日 星期四

vb.net 讀寫ini檔 [Read Write ini File]


1
2
3
4
5
6
7
Imports System.IO

 

    Private Declare Unicode Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringW" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpString As String, ByVal lpFileName As String) As Int32

    Private Declare Unicode Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringW" (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



1
2
3
4
5
    Private Sub writeIni(ByVal iniFileName As String, ByVal Section As String, ByVal ParamName As String, ByVal ParamVal As String)

        Dim Result As Integer = WritePrivateProfileString(Section, ParamName, ParamVal, iniFileName)

    End Sub



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
    Private Function ReadIni(ByVal IniFileName As String, ByVal Section As String, ByVal ParamName As String, ByVal ParamDefault As String) As String

        Dim ParamVal As String = Space$(1024)

        Dim LenParamVal As Long = GetPrivateProfileString(Section, ParamName, ParamDefault, ParamVal, Len(ParamVal), IniFileName)

        'ReadIni = Left$(ParamVal, LenParamVal)

    ReadIni = ParamVal

    End Function

---------------------- FORM ----------------------


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
    Dim File = Application.StartupPath + "\Test.ini"

    Dim Section = "Settings"

    Dim Author = "Author"

    Dim org = "Organization"


    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click

        TextBox1.Text = ReadIni(File, Section, Author, "")

        TextBox2.Text = ReadIni(File, Section, org, "")

    End Sub


    Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click

        writeIni(File, Section, Author, TextBox3.Text)

        writeIni(File, Section, org, TextBox4.Text)

    End Sub

沒有留言:

張貼留言