Cari Artikel Lain

Jumat, 09 Oktober 2009

Membaca dan menulis file .log atau .txt pada VB.net

Untuk memberikan report atau laporan yang berhubungan dengan berjalannya suatu program biasanya menggunakan file .log atau .txt. Bagaimana menulis dan membaca file tersebut dalam VB.Net ?
 
 
Membaca file .log

Dim nom As Object

Dim strTeks(2) As String

nom = FreeFile()

If Dir(My.Application.Info.DirectoryPath & "\re" & Format(Now, "yyMM") & ".log") <> "" Then

FileOpen(1, My.Application.Info.DirectoryPath & "\re" & Format(Now, "yyMM") & ".log", OpenMode.Binary)

strTeks(0) = InputString(1, LOF(1))

FileClose(1)

End If

TextBox1.text = strTeks(0)

 

Menulis file .log

Dim nom As Object

nom = FreeFile()

FileOpen(nom, My.Application.Info.DirectoryPath & "\re" & Format(Now, "yyMM") & ".log", OpenMode.Output)

PrintLine(nom, "Isi data teksnya disini")

FileClose(nom)

 

Semoga bermanfaat untuk semuanya.
 
 

Membaca file .INI pada VB.Net

File .ini merupakan cara standar untuk menyimpan setting program pada kebanyakan program. Tetapi bagaimana cara membaca file .ini menggunakan Vb.net ? Berikut script yang saya dapat di internet dengan sedikit modifikasi  untuk memudahkan penggunaan.
 

Option Strict On

Module INIAccess

#Region "API Calls"

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

#End Region

Public Overloads Function INIRead(ByVal INIPath As String, _

ByVal SectionName As String, ByVal KeyName As String, _

ByVal DefaultValue As String) As String

Dim n As Int32

Dim sData As String

sData = Space$(1024)

n = GetPrivateProfileString(SectionName, KeyName, DefaultValue, _

sData, sData.Length, INIPath)

If n > 0 Then

INIRead = sData.Substring(0, n)

Else

INIRead = ""

End If

End Function

#Region "INIRead Overloads"

Public Overloads Function INIRead(ByVal INIPath As String, _

ByVal SectionName As String, ByVal KeyName As String) As String

Return INIRead(INIPath, SectionName, KeyName, "")

End Function

Public Overloads Function INIRead(ByVal INIPath As String, _

ByVal SectionName As String) As String

Return INIRead(INIPath, SectionName, Nothing, "")

End Function

Public Overloads Function INIRead(ByVal INIPath As String) As String

Return INIRead(INIPath, Nothing, Nothing, "")

End Function

#End Region

Public Sub INIWrite(ByVal INIPath As String, ByVal SectionName As String, _

ByVal KeyName As String, ByVal TheValue As String)

Call WritePrivateProfileString(SectionName, KeyName, TheValue, INIPath)

End Sub

Public Overloads Sub INIDelete(ByVal INIPath As String, ByVal SectionName As String, _

ByVal KeyName As String)

Call WritePrivateProfileString(SectionName, KeyName, Nothing, INIPath)

End Sub

Public Overloads Sub INIDelete(ByVal INIPath As String, ByVal SectionName As String)

Call WritePrivateProfileString(SectionName, Nothing, Nothing, INIPath)

End Sub

Public Function BacaIni(ByVal Section As String, ByVal Kunci As String, ByVal IsiDefault As String) As String

Dim Sisi As String

Sisi = INIRead(My.Application.Info.DirectoryPath & "\set.conf", Section, Kunci, IsiDefault)

BacaIni = Sisi

End Function

Public Sub TulisIni(ByVal Section As String, ByVal Kunci As String, ByVal Datanya As String)

INIWrite(My.Application.Info.DirectoryPath & "\set.conf", Section, Kunci, Datanya)

End Sub

End Module

 
Pada Form sisipkan script berikut untuk membaca file
 
Dim IsiIni as string

IsiIni = BacaIni("Section", "Kunci", "NGGAK ADA")

 

Pada Form sisipkan script berikut untuk menulis file

TulisIni("Section", "Kunci", "Isi datanya disini")

 
Semoga tulisan ini bermanfaat