Sprache: VB
Diese Funktionen ließt und schreibt Textdateien in verscheidene Formate.
Sollten selbtserklärend sein.
Falls nicht, bitte melden ;)
Änderungen von Tim Hartwig eingefügt (siehe Kommentar)
Public Function TXT2ListOfString(ByVal FullPath As String) As List(Of String)
Dim lst As New List(Of String)
If IO.File.Exists(FullPath) Then lst.AddRange(IO.File.ReadAllLines(FullPath, System.Text.Encoding.Default))
Return lst
End Function
Public Function ListOfString2TXT(ByVal FullPath As String, ByVal lst As List(Of String)) As Boolean
Dim f As New IO.FileInfo(FullPath)
If f.Directory.Exists = True Then
IO.File.WriteAllLines(FullPath, lst.ToArray, System.Text.Encoding.Default)
Return True
End If
Return False
End Function
Public Function TXT2StringArray(ByVal FullPath As String) As String()
Dim strar() As String = Nothing
If IO.File.Exists(FullPath) Then strar = IO.File.ReadAllLines(FullPath, System.Text.Encoding.Default)
Return strar
End Function
Public Function StringArray2TXT(ByVal FullPath As String, ByVal strar() As String) As Boolean
Dim f As New IO.FileInfo(FullPath)
If f.Directory.Exists = True Then
IO.File.WriteAllLines(FullPath, strar, System.Text.Encoding.Default)
Return True
End If
Return False
End Function
Public Function TXT2ByteArray(ByVal FullPath As String) As Byte()
Dim byAr() As Byte = Nothing
If IO.File.Exists(FullPath) Then byAr = IO.File.ReadAllBytes(FullPath)
Return byAr
End Function
Public Function ByteArray2TXT(ByVal FullPath As String, ByVal byAr() As Byte) As Boolean
Dim f As New IO.FileInfo(FullPath)
If f.Directory.Exists = True Then
IO.File.WriteAllBytes(FullPath, byAr)
Return True
End If
Return False
End Function
Public Function TXT2String(ByVal FullPath As String) As String
Dim str As String = ""
If IO.File.Exists(FullPath) Then str = IO.File.ReadAllText(FullPath, System.Text.Encoding.Default)
Return str
End Function
Public Function String2TXT(ByVal FullPath As String, ByVal str As String) As Boolean
Dim f As New IO.FileInfo(FullPath)
If f.Directory.Exists = True Then
IO.File.WriteAllText(FullPath, str, System.Text.Encoding.Default)
Return True
End If
Return False
End Function
Public Function TXT2ListOfString(ByVal FullPath As String) As List(Of String)
Dim lst As New List(Of String)
If IO.File.Exists(FullPath) Then lst.AddRange(IO.File.ReadAllLines(FullPath, System.Text.Encoding.Default))
Return lst
End Function
Public Function ListOfString2TXT(ByVal FullPath As String, ByVal lst As List(Of String)) As Boolean
Dim f As New IO.FileInfo(FullPath)
If f.Directory.Exists = True Then
IO.File.WriteAllLines(FullPath, lst.ToArray, System.Text.Encoding.Default)
Return True
End If
Return False
End Function
Public Function TXT2StringArray(ByVal FullPath As String) As String()
Dim strar() As String = Nothing
If IO.File.Exists(FullPath) Then strar = IO.File.ReadAllLines(FullPath, System.Text.Encoding.Default)
Return strar
End Function
Public Function StringArray2TXT(ByVal FullPath As String, ByVal strar() As String) As Boolean
Dim f As New IO.FileInfo(FullPath)
If f.Directory.Exists = True Then
IO.File.WriteAllLines(FullPath, strar, System.Text.Encoding.Default)
Return True
End If
Return False
End Function
Public Function TXT2ByteArray(ByVal FullPath As String) As Byte()
Dim byAr() As Byte = Nothing
If IO.File.Exists(FullPath) Then byAr = IO.File.ReadAllBytes(FullPath)
Return byAr
End Function
Public Function ByteArray2TXT(ByVal FullPath As String, ByVal byAr() As Byte) As Boolean
Dim f As New IO.FileInfo(FullPath)
If f.Directory.Exists = True Then
IO.File.WriteAllBytes(FullPath, byAr)
Return True
End If
Return False
End Function
Public Function TXT2String(ByVal FullPath As String) As String
Dim str As String = ""
If IO.File.Exists(FullPath) Then str = IO.File.ReadAllText(FullPath, System.Text.Encoding.Default)
Return str
End Function
Public Function String2TXT(ByVal FullPath As String, ByVal str As String) As Boolean
Dim f As New IO.FileInfo(FullPath)
If f.Directory.Exists = True Then
IO.File.WriteAllText(FullPath, str, System.Text.Encoding.Default)
Return True
End If
Return False
End Function
Alte URL:
/snippet/textdatei-lesen-und-schreiben-string-stringarray-etc/818
Es ist ja schön und gut das du vorhandene Funktionen nochmal als extra Funktion kapselst mit einer schönen File.Exists abfrage aber gebe dem User dann doch noch bitte die Möglichkeit einen Encoder anzugeben oder willst du das wenn jemand eine Textdatei mit Umlauten (A,Ö,Ü) einlesen will das diese Umlaute verloren gehn? Denn dafür müsste der Default Encoder angegeben werden.
So habs geändert 😉
Hatte das einfach übersehen, da ich es aus einem meiner aktuellen Projekte gebilded habe.
Ja ich weis, dass auch das wieder „nur“ Standardfunktionen sind, aber wenn man gerade anfängt, ist ein Beispiel der Möglichkeiten nicht schlecht.
Außerdem bin ich es auch leid, solche sachen in allenmöglichen Projekten wieder und wieder neu zu erfinden/den selben misst zu tippen ^^