venerdì 16 settembre 2011

How To: Read Write File Vb.Net

Dato che sono sempre stato estermamente curioso mi sono deciso a fare qualche passo in un mondo abbastanza vicino al c#... vicino ma non vicinissimo.

Ecco quindi una serie di post specifici per il cugino Vb.Net

Perchè non iniziare con qualcosa di semplice ?? ecco come leggere e scrivere un
file con Vb.Net ( pseudo traduzione da C# )

Imports System.IO


Public Class IOSamples

Public Function WriteFile(ByVal FileName As String,
ByVal outputText As String) As Boolean
If FileName = "" Then
WriteFile = False
End If

If Not File.Exists(FileName) Then
Dim tw As New StreamWriter(FileName)

tw.Write(outputText)

tw.Close()

tw = Nothing

End If

WriteFile = True

End Function


Public Function ReadFile(ByVal fileName As String) As String

Dim inputText As String = ""

If fileName = "" Then
ReadFile = ""
End If


If File.Exists(fileName) Then
Dim tr As New StreamReader(fileName)

inputText = tr.ReadToEnd()
tr.Close()

tr = Nothing

End If

ReadFile = inputText
End Function
End Class

Nessun commento: