반응형

* INI 파일 예제

 

메인화면

Form1.vb

 

Imports System.Runtime.InteropServices
Imports System.IO
Imports System.Text

Public Class Form1
    
    '빌드되는 폴더 경로 가져오기...
    Dim strCheckFolder As String = Application.ExecutablePath.Substring(0, Application.ExecutablePath.LastIndexOf("\"))

#Region "INI File...API 선언 및 함수 선언"
    <DllImport("kernel32.dll", SetLastError:=True)> _
    Private Shared Function GetPrivateProfileString(ByVal lpAppName As String, _
                                ByVal lpKeyName As String, _
                                ByVal lpDefault As String, _
                                ByVal lpReturnedString As StringBuilder, _
                                ByVal nSize As Integer, _
                                ByVal lpFileName As String) As Integer
    End Function

    <DllImport("kernel32.dll", SetLastError:=True)> _
    Private Shared Function WritePrivateProfileString(ByVal lpAppName As String, _
                            ByVal lpKeyName As String, _
                            ByVal lpString As String, _
                            ByVal lpFileName As String) As Boolean
    End Function

    Private Function SetINI(ByVal strAppName As String, _
                           ByVal strKey As String, _
                           ByVal strValue As String, _
                           ByVal strFilePath As String) As Boolean
        SetINI = WritePrivateProfileString(strAppName, strKey, strValue, strFilePath)
    End Function
    Private Function GetINI(ByVal strAppName As String, _
                           ByVal strKey As String, _
                           ByVal strValue As String, _
                           ByVal strFilePath As String) As String
        Dim strbTmp As StringBuilder = New StringBuilder(255)

        GetPrivateProfileString(strAppName, strKey, strValue, strbTmp, strbTmp.Capacity, strFilePath)

        GetINI = strbTmp.ToString()

    End Function

    Private Function Create_INIFile(ByVal strPath As String, _
                                   ByVal strFileName As String) As Boolean
        If Dir(strPath & "\" & strFileName) <> "" Then
            Exit Function
        End If

        Try
            Using sw As StreamWriter = New StreamWriter(strPath & "\" & strFileName, False)
                sw.WriteLine(vbCrLf)
                sw.Flush()
                sw.Close()
            End Using
        Catch ex As Exception

            Return False
        End Try

        Return True
    End Function

#End Region


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'INIFile Create
        If Create_INIFile(strCheckFolder, "Test.ini") Then
            Label1.Text = "INIFile Create Complete."
            Label2.Text = "FileName: Test.ini"
        End If

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        'INIFile Read
        Label4.Text = "INIFile Read Complete."
        Label3.Text = "Value: " + GetINI("Test_Info", "Test", "", strCheckFolder + "\Test.ini")
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        'INIFile Write
        If SetINI("Test_Info", "Test", "1231231231", strCheckFolder + "\Test.ini") Then
            Label6.Text = "INIFile Write Complete."
            Label5.Text = "Value: 1231231231"
        End If

    End Sub

End Class

위 예제는 윈도우 API 함수를 이용해 INI 파일을 읽고 쓰기를 해 보았습니다. INI 파일 삭제는

'파일이 있으면...

If File.Exits("파일위치") then

      '파일 삭제 

      File.Delete("파일위치")

End If

이 코드만 있으면 파일이 삭제 가능 합니다.

 

* 예제 결과

 

예제 결과 화면

 

반응형

+ Recent posts