반응형
* 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
이 코드만 있으면 파일이 삭제 가능 합니다.
* 예제 결과
반응형
'VB.NET Programming' 카테고리의 다른 글
[VBNET] Excel File Read & Write 예제 (0) | 2019.09.29 |
---|---|
[VBNET] File Create Delete Read Write Ex (0) | 2019.09.27 |
[VBNET] 레지스트리(Registry) Create & Delete & Read & Write (0) | 2019.09.23 |
[VBNET] 객체 직렬화 및 파일로 저장, 불러오기( Serialization) (0) | 2019.09.20 |
[VBNET] BackGround Worker 를 이용 백그라운드 스레드 (0) | 2019.09.17 |