How to Create a text file, then readline and writeline it?School Assignment, need answer asap
Page 1 of 1
14 Replies - 47227 Views - Last Post: 27 May 2012 - 05:48 AM
#1
How to Create a text file, then readline and writeline it?
Posted 13 September 2008 - 02:11 AM
I needed to create a text file into "C:\Program Files\User\User.text"
But if the file exist it will read line1, line 2 and line3 of the text in different textboxes
Then if I click save, it will write textbox 1 to line 1, textbox2 to line 2 and textbox3 to line 3.
How do I do it?
Thanks in advance
Replies To: How to Create a text file, then readline and writeline it?
#2
Re: How to Create a text file, then readline and writeline it?
Posted 13 September 2008 - 02:25 AM
a simple google search brings up tons of pages
http://support.microsoft.com/kb/315828
for instance
#3
Re: How to Create a text file, then readline and writeline it?
Posted 13 September 2008 - 02:29 AM
#4
Re: How to Create a text file, then readline and writeline it?
Posted 13 September 2008 - 02:36 AM
The last code that I used is:
If File.Exists("C:\Program Files\TIUser\TextFile.dat") Then MessageBox.Show("File Already Exist") Else System.IO.File.Create("C:\Program Files\TIUser\TextFile.dat") End If
But error appears saying that "cannot find path
This post has been edited by ultrajet: 13 September 2008 - 02:38 AM
#5
Re: How to Create a text file, then readline and writeline it?
Posted 13 September 2008 - 02:54 AM
Dim objStreamWriter As StreamWriter 'Pass the file path and the file name to the StreamWriter constructor. objStreamWriter = New StreamWriter("C:\Testfile.txt") 'Write a line of text. objStreamWriter.WriteLine("Hello World") 'Write a second line of text. objStreamWriter.WriteLine("From the StreamWriter class") 'Close the file. objStreamWriter.Close()
theres an option to append as well
So that should solve your problem, if the file exists it will add to the end of the file. if it doesn't it will create the file
#6
Re: How to Create a text file, then readline and writeline it?
Posted 13 September 2008 - 02:58 AM
But is there any way for me to create a non existing directory?
Example I wanted to put it on "C:\User\user.txt" even if the directory does not exist?
#7
Re: How to Create a text file, then readline and writeline it?
Posted 13 September 2008 - 03:29 AM
Dim dir As String = "c:\Directory" My.Computer.FileSystem.CreateDirectory(dir)
#8
Re: How to Create a text file, then readline and writeline it?
Posted 13 September 2008 - 03:45 AM
Check the red one.
Here is the code:
Public Class Form2 Dim objStreamWriter As StreamWriter Dim objStreamWriter1 As StreamWriter Dim objStreamWriter2 As StreamWriter Dim objStreamWriter3 As StreamWriter Dim strLine As String Dim objStreamReader As StreamReader Dim objStreamReader1 As StreamReader Dim objStreamReader2 As StreamReader Dim objStreamReader3 As StreamReader Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click If File.Exists("C:\user.txt") Then MessageBox.Show("Account Already Exist") Else objStreamWriter = New StreamWriter("C:\user.txt") objStreamWriter1 = New StreamWriter("C:\user1.txt") objStreamWriter2 = New StreamWriter("C:\user2.txt") objStreamWriter3 = New StreamWriter("C:\user3.txt") objStreamWriter.Close() objStreamWriter1.Close() objStreamWriter2.Close() objStreamWriter3.Close() End If Try objStreamReader = New StreamReader("C:\user.txt") objStreamReader1 = New StreamReader("C:\user1.txt") objStreamReader2 = New StreamReader("C:\user2.txt") objStreamReader3 = New StreamReader("C:\user3.txt") Admin.Text = objStreamReader.ReadLine Sales.Text = objStreamReader1.ReadLine Purchaser.Text = objStreamReader2.ReadLine HRD.Text = objStreamReader3.ReadLine objStreamReader.Close() objStreamReader1.Close() objStreamReader2.Close() objStreamReader3.Close() Catch ex As Exception MessageBox.Show("Account have been made.") End Try [color=#FF0000] Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click objStreamWriter = New StreamWriter("C:\user.txt", False) objStreamWriter1 = New StreamWriter("C:\user1.txt", False) objStreamWriter2 = New StreamWriter("C:\user2.txt", False) objStreamWriter3 = New StreamWriter("C:\user3.txt", False) objStreamWriter.WriteLine(Admin.Text) objStreamWriter1.WriteLine(Sales.Text) objStreamWriter2.WriteLine(Purchaser.Text) objStreamWriter3.WriteLine(HRD.Text) MessageBox.Show("Password changed")[/color] End Sub
End Sub
This post has been edited by ultrajet: 13 September 2008 - 03:50 AM
#9
Re: How to Create a text file, then readline and writeline it?
Posted 13 September 2008 - 03:47 AM
and what error are you getting?
this is an example of how to use a streamreader
Dim reader As StreamReader = New StreamReader(winDir & "\system.ini") Try Me.ListBox1.Items.Clear() Do 'Until reader.Peek = -1 Me.ListBox1.Items.Add(reader.ReadLine) Loop Until reader.Peek = -1 Catch Me.ListBox1.Items.Add("File is empty")
This post has been edited by Damage: 13 September 2008 - 03:52 AM
#10
Re: How to Create a text file, then readline and writeline it?
Posted 13 September 2008 - 03:56 AM
The problem is that the text does not appear on the created text.
For example
after these codes are executed, nothing still appears on the user.txt, user1.txt, etc
Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click objStreamWriter = New StreamWriter("c:\Program Files\user\user.txt", False) objStreamWriter1 = New StreamWriter("c:\Program Files\user\user1.txt", False) objStreamWriter2 = New StreamWriter("c:\Program Files\user\user2.txt", False) objStreamWriter3 = New StreamWriter("c:\Program Files\user\user3.txt", False) Admin.Text = strLine Sales.Text = strLine1 Purchaser.Text = strLine2 HRD.Text = strLine3 objStreamWriter.Write(strLine) objStreamWriter1.Write(strLine1) objStreamWriter2.Write(strLine2) objStreamWriter3.Write(strLine3) MessageBox.Show("Password changed") End Sub
#11
Re: How to Create a text file, then readline and writeline it?
Posted 13 September 2008 - 03:57 AM
sorry came in after your edit
Admin.Text = strLine Sales.Text = strLine1 Purchaser.Text = strLine2 HRD.Text = strLine3
this is wrong. your assigning the value in strline(which I'm assuming is blank) to the textbox.
strLine= Admin.Text strLine1 = Sales.Text strLine2 = Purchaser.Text strLine3 = HRD.Text
This post has been edited by Damage: 13 September 2008 - 04:05 AM
#12
Re: How to Create a text file, then readline and writeline it?
Posted 13 September 2008 - 04:11 AM
It finally worked!
#13
Re: How to Create a text file, then readline and writeline it?
Posted 13 September 2008 - 04:15 AM

but for future reference, seriously just spend a couple of minutes googling it, it can often save you hours of headache

#14
Re: How to Create a text file, then readline and writeline it?
Posted 26 May 2012 - 09:26 AM
' Read and write ini files. Public Class InIFileControler Private iniPath As String = "" Private keyValueSeparator As String = "" Public Sub New(ByVal _iniPath As String, Optional ByVal _keyValueSeparator As String = "=") iniPath = _iniPath keyValueSeparator = _keyValueSeparator End Sub ' Returns the path without file name, if the complete path with file name is provided. ' ie: ' Provided: c:\somefolder\somefolder\somefilename.exe ' Returned: c:\somefolder\somefolder\ Private Function GetFolderFromPath(ByVal completePath As String) As String Dim parts() As String Dim count As Int32 = 0 GetFolderFromPath = "" parts = Split(completePath, "\") For count = 0 To parts.Length - 2 GetFolderFromPath = GetFolderFromPath & parts(count) & "\" Next End Function Private Function CreateFolders(ByVal _path As String) As Boolean CreateFolders = True Dim parts() As String Dim path As String = "" Dim count As Int32 parts = Split(_path, "\") path = parts(0) For count = 1 To parts.Length - 1 path += "\" & parts(count) Try If Not Directory.Exists(path) Then Directory.CreateDirectory(path) End If Catch ex As Exception End Try Next End Function Private Function PauseWithoutBlockingUI(ByVal M As Double) As Boolean Dim timeOut As DateTime = Now.AddMilliseconds(M) Do Application.DoEvents() Loop Until Now > timeOut End Function ' Get the contents of the ini file. Public Function Read(ByRef iniEntries As ArrayList, Optional ByRef errorMessage As String = "") As Boolean If Not File.Exists(iniPath) Then errorMessage = "Ini file not found." Return False End If Try ' Read the ini file into an arraylist... Dim reader As StreamReader = New StreamReader(iniPath) iniEntries.Clear() Do Until reader.Peek = -1 'Until eof iniEntries.Add(reader.ReadLine) Loop reader.Close() Catch ex As Exception ' The file's empty. End Try Return True End Function ' Error Message will be in theValue on failure. Public Function GetValue(ByVal theKey As String, ByRef theValue As String) As Boolean Dim iniEntries As New ArrayList Dim count As Int32 = 0 Dim parts() As String = Nothing Dim tmp As String = "" Dim tmpKey As String = "" Dim tmpValue As String = "" ' Get the contents of the ini file. If Not Read(iniEntries, tmp) Then theValue = tmp Return False End If ' search for the key... If iniEntries.Count > 0 Then For count = 0 To iniEntries.Count - 1 tmp = CStr(iniEntries.Item(count)) parts = Split(tmp, keyValueSeparator) tmpKey = parts(0) tmpValue = parts(1) ' Have we found it? If theKey.ToLower.Trim = tmpKey.ToLower.Trim Then theValue = tmpValue Return True End If Next theValue = "Entry not found." Else theValue = "No entries found in the ini file." End If Return False End Function Public Function Write(ByVal theKey As String, ByVal theValue As String, _ Optional ByRef errorMessage As String = "") As Boolean Dim iniEntries As New ArrayList Dim count As Int32 = 0 Dim tmp As String = "" Dim parts() As String = Nothing Dim tmpKey As String = "" Dim tmpValue As String = "" Dim valueWritten As Boolean = False ' If ini file doesn't exist, create it. If Not File.Exists(iniPath) Then Try CreateFolders(GetFolderFromPath(iniPath)) Dim fs As New FileStream(iniPath, FileMode.Create) fs.Close() Catch ex As Exception errorMessage = "Could not create ini file: " & iniPath Return False End Try End If ' Get the contents of the ini file, and ' report an error reading from it, if any. If Not Read(iniEntries, tmp) Then errorMessage = tmp Return False End If ' Delete the original ini file. Try File.Delete(iniPath) Catch ex As Exception errorMessage = ex.Message Return False End Try ' Wait for it to be deleted... While File.Exists(iniPath) PauseWithoutBlockingUI(10) End While Try Dim writer As New StreamWriter(iniPath, False) If iniEntries.Count < 1 Then writer.WriteLine("" & theKey & keyValueSeparator & theValue & "") valueWritten = True Else For count = 0 To iniEntries.Count - 1 tmp = CStr(iniEntries.Item(count)) parts = Split(tmp, keyValueSeparator) tmpKey = parts(0) tmpValue = parts(1) ' Modify ini file if the key already exists. If theKey.ToLower.Trim = tmpKey.ToLower.Trim Then writer.WriteLine("" & theKey & keyValueSeparator & theValue & "") valueWritten = True Else writer.WriteLine(tmp) End If ' Add it to the ini file if the key didn't exist in it already If Not valueWritten Then writer.WriteLine("" & theKey & keyValueSeparator & theValue & "") valueWritten = True End If Next End If writer.Close() Catch ex As Exception errorMessage = "Error writing in file." & vbCrLf & vbCrLf & ex.Message Return False End Try Return valueWritten End Function End Class
#15
Re: How to Create a text file, then readline and writeline it?
Posted 27 May 2012 - 05:48 AM
Public Class InIFileControler Private iniPath As String = "" Private keyValueSeparator As String = "" Public Sub New(ByVal _iniPath As String, Optional ByVal _keyValueSeparator As String = "=") iniPath = _iniPath keyValueSeparator = _keyValueSeparator End Sub ' Returns the path without file name, if the complete path with file name is provided. ' ie: ' Provided: c:\somefolder\somefolder\somefilename.exe ' Returned: c:\somefolder\somefolder\ Private Function GetFolderFromPath(ByVal completePath As String) As String Dim parts() As String Dim count As Int32 = 0 GetFolderFromPath = "" parts = Split(completePath, "\") For count = 0 To parts.Length - 2 GetFolderFromPath = GetFolderFromPath & parts(count) & "\" Next End Function Private Function CreateFolders(ByVal _path As String) As Boolean CreateFolders = True Dim parts() As String Dim path As String = "" Dim count As Int32 parts = Split(_path, "\") path = parts(0) For count = 1 To parts.Length - 1 path += "\" & parts(count) Try If Not Directory.Exists(path) Then Directory.CreateDirectory(path) End If Catch ex As Exception End Try Next End Function Private Function PauseWithoutBlockingUI(ByVal M As Double) As Boolean Dim timeOut As DateTime = Now.AddMilliseconds(M) Do Application.DoEvents() Loop Until Now > timeOut End Function Public Function ClearINI(Optional ByVal errorMessage As String = "") As Boolean Try File.Delete(iniPath) Catch ex As Exception errorMessage = ex.Message Return False End Try Try Dim newIni As New FileStream(iniPath, FileMode.Create) newIni.Close() Catch ex As Exception errorMessage = ex.Message Return False End Try Return True End Function ' Get the contents of the ini file. Public Function ReadAllEntries(ByRef iniEntries As ArrayList, Optional ByRef errorMessage As String = "") As Boolean If Not File.Exists(iniPath) Then errorMessage = "Ini file not found." Return False End If Try ' Read the ini file into an arraylist... Dim reader As StreamReader = New StreamReader(iniPath) iniEntries.Clear() Do Until reader.Peek = -1 'Until eof iniEntries.Add(reader.ReadLine) Loop reader.Close() Catch ex As Exception ' The file's empty. End Try Return True End Function ' Error Message will be in theValue on failure. Public Function GetValue(ByVal theKey As String, ByRef theValue As String) As Boolean Dim iniEntries As New ArrayList Dim count As Int32 = 0 Dim parts() As String = Nothing Dim tmp As String = "" Dim tmpKey As String = "" Dim tmpValue As String = "" ' Get the contents of the ini file. If Not ReadAllEntries(iniEntries, tmp) Then theValue = tmp Return False End If ' search for the key... If iniEntries.Count > 0 Then For count = 0 To iniEntries.Count - 1 tmp = CStr(iniEntries.Item(count)) parts = Split(tmp, keyValueSeparator) tmpKey = parts(0) tmpValue = parts(1) ' Have we found it? If theKey.ToLower.Trim = tmpKey.ToLower.Trim Then theValue = tmpValue Return True End If Next theValue = "Entry not found." Else theValue = "No entries found in the ini file." End If Return False End Function Public Function RemoveEntry(ByVal theKey As String, ByVal theValue As String, _ Optional ByRef errorMessage As String = "") As Boolean Dim iniEntries As New ArrayList Dim count As Int32 = 0 Dim tmp As String = "" Dim parts() As String = Nothing Dim tmpKey As String = "" Dim tmpValue As String = "" Dim entryRemoved As Boolean = False ' Get the contents of the ini file, and ' report an error reading from it, if any. If Not ReadAllEntries(iniEntries, tmp) Then errorMessage = tmp Return False End If ' Delete the original ini file. Try File.Delete(iniPath) Catch ex As Exception errorMessage = ex.Message Return False End Try ' Wait for it to be deleted... While File.Exists(iniPath) PauseWithoutBlockingUI(10) End While Try Dim writer As New StreamWriter(iniPath, False) If iniEntries.Count < 1 Then errorMessage = "Entry does not exist." Return False Else For count = 0 To iniEntries.Count - 1 tmp = CStr(iniEntries.Item(count)) parts = Split(tmp, keyValueSeparator) tmpKey = parts(0) tmpValue = parts(1) ' If we find the entry, If theKey.ToLower.Trim = tmpKey.ToLower.Trim Then ' Leave it out. entryRemoved = True Else writer.WriteLine(tmp) End If Next End If writer.Close() Catch ex As Exception errorMessage = "Error writing ini file." & vbCrLf & vbCrLf & ex.Message Return False End Try If Not entryRemoved Then errorMessage = "Entry does not exist." Return entryRemoved End Function Public Function WriteEntry(ByVal theKey As String, ByVal theValue As String, _ Optional ByRef errorMessage As String = "") As Boolean Dim iniEntries As New ArrayList Dim count As Int32 = 0 Dim tmp As String = "" Dim parts() As String = Nothing Dim tmpKey As String = "" Dim tmpValue As String = "" Dim valueWritten As Boolean = False ' If ini file doesn't exist, create it. If Not File.Exists(iniPath) Then Try CreateFolders(GetFolderFromPath(iniPath)) Dim fs As New FileStream(iniPath, FileMode.Create) fs.Close() Catch ex As Exception errorMessage = "Could not create ini file: " & iniPath Return False End Try End If ' Get the contents of the ini file, and ' report an error reading from it, if any. If Not ReadAllEntries(iniEntries, tmp) Then errorMessage = tmp Return False End If ' Delete the original ini file. Try File.Delete(iniPath) Catch ex As Exception errorMessage = ex.Message Return False End Try ' Wait for it to be deleted... While File.Exists(iniPath) PauseWithoutBlockingUI(10) End While Try Dim writer As New StreamWriter(iniPath, False) If iniEntries.Count < 1 Then writer.WriteLine("" & theKey & keyValueSeparator & theValue & "") valueWritten = True Else For count = 0 To iniEntries.Count - 1 tmp = CStr(iniEntries.Item(count)) parts = Split(tmp, keyValueSeparator) tmpKey = parts(0) tmpValue = parts(1) ' Modify ini file if the key already exists. If theKey.ToLower.Trim = tmpKey.ToLower.Trim Then writer.WriteLine("" & theKey & keyValueSeparator & theValue & "") valueWritten = True Else writer.WriteLine(tmp) End If Next ' Add it to the ini file if the key didn't exist in it already If Not valueWritten Then writer.WriteLine("" & theKey & keyValueSeparator & theValue & "") valueWritten = True End If End If writer.Close() Catch ex As Exception errorMessage = "Error writing ini file." & vbCrLf & vbCrLf & ex.Message Return False End Try Return valueWritten End Function End Class