VB2010 read and write files
-
Saturday, July 10, 2010 12:15 AMVB 21010 How to load an array fron a txt file and write to it also?
All Replies
-
Saturday, July 10, 2010 12:52 AM
Try these links on how to read and write text file
http://www.homeandlearn.co.uk/NET/nets8p3.html
http://support.microsoft.com/kb/304427
kaymaf
CODE CONVERTER SITE
-
Saturday, July 10, 2010 3:27 AM
Hi! Justrc
Try this code:
' This is to read all file content in vb2010
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim File As New StreamReader("example.txt", System.Text.Encoding.UTF8)
TextBox1.Text = File.ReadToEnd()
File.Close()
End Sub
'This is to write in the file
Dim appendMode As Boolean = True
Dim File As New StreamWriter("example.txt", appendMode, System.Text.Encoding.UTF8)
File.Write(TextBox1.Text)
File.Close()
Naldo -
Saturday, July 10, 2010 3:50 AM
In addition to kaymaf's suggestion, you might also consider the following throw-away code:
Module Module1 ''' <summary> ''' Read "someFile.txt" increment each letter or digit by one and ''' output to "somefile-out.txt" ''' </summary> ''' <remarks> ''' shawn.eary 09-JUL-2010: This code is public domain ''' </remarks> Sub Main() ' Read the information into a single string Dim objReader As New System.IO.StreamReader("./somefile.txt") Dim fileString As String = objReader.ReadToEnd() objReader.Close() objReader.Dispose() ' Convert the input string into an array Dim theArray As Char() = fileString.ToCharArray() ' Declare a resultString to hold the value Dim resultString As String = "" ' Increment each item in the string Dim curChar As Char For i As Integer = 0 To (theArray.Length - 1) curChar = theArray(i) If (Char.IsLetterOrDigit(curChar)) Then theArray(i) = Chr(Asc(curChar) + 1) End If Next ' Convert the modified array back to a string Dim newString As String = New String(theArray) ' Write the array to somefile-out.txt Dim objWriter As New System.IO.StreamWriter("./somefile-out.txt") objWriter.Write(newString) objWriter.Close() objWriter.Dispose() End Sub End Module
- Marked As Answer by Justrc Saturday, July 10, 2010 6:08 AM
-
Saturday, July 10, 2010 12:25 PM
I will just throw out my favorite, where lines is the array desired.dim lines as string()lines = File.ReadAllLines("Somefilename.txt")=and=File.WriteAllLines("Somefilename.txt", lines)
--
Mike

