|
-
Aug 23rd, 2011, 03:03 PM
#1
Thread Starter
New Member
File handling.
Hello people. Im learning Visual Basic 2010 on my programming class and we reached the part of file handling. Here i tried to make a simple program that can create a text file with the content of what i type on the textbox, basically i create the file if it doesn't exist, and if it already exists i append the content to it. Once the file is saved i can read it using the "Open" button. Unfortunataly, whenever i try to create the file, the compiler gives me an error saying that it cannot modify a closed file, although i've followed up the execution instruction by instruction and i can't find the error. Can some of you help me?...
Code:
Imports System.IO
Public Class Form1
Dim body As String
Private Sub bt_open_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bt_open.Click
Dim sample As New FileStream(Application.StartupPath & tb_filename.Text, FileMode.Open, FileAccess.Read)
Dim reader As New StreamReader(sample)
body = reader.ReadLine
Do While body IsNot Nothing
tb_text.Text = tb_text.Text + body
body = reader.ReadLine
Loop
sample.Close()
reader.Close()
End Sub
Private Sub bt_save_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bt_save.Click
If File.Exists(tb_filename.Text) Then
Dim sample As New FileStream(Application.StartupPath & tb_filename.Text, FileMode.Append, FileAccess.Write, FileShare.ReadWrite)
Dim writer As New StreamWriter(sample)
body = tb_text.Text
writer.Write(body)
sample.Close()
writer.Close()
Else
Dim sample As New FileStream(Application.StartupPath & tb_filename.Text, FileMode.CreateNew, FileAccess.Write, FileShare.ReadWrite)
Dim writer As New StreamWriter(sample)
body = tb_text.Text
writer.Write(body)
sample.Close()
writer.Close()
End If
End Sub
Private Sub tb_filename_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles tb_filename.Click
MsgBox("Please put the complete name including file extention", MsgBoxStyle.Information, "Attention")
End Sub
End Class
Hope you can help me... Thanks from now on for your time!...
-
Aug 23rd, 2011, 05:39 PM
#2
Fanatic Member
Re: File handling.
i'm assuming because up at the top in your open, you're closing sample and reader...
then in another button, you are attempting to reuse it.
I realize you are using the new statement, but i suggest you try changing those names slightly to see if that works.
However, if I were you, (IDK if you have to follow their specific code or not), but...
I would use:
vb Code:
'Code Removed 'Read next post
I just did exactly what you're doing... but in 3 lines of code.
Last edited by TCarter; Aug 23rd, 2011 at 05:44 PM.
-
Aug 23rd, 2011, 05:44 PM
#3
Fanatic Member
Re: File handling.
Modified the code...
vb Code:
'Determins if the file exists... Dim fileExists As Boolean = fileExists = My.Computer.FileSystem.FileExists("C:\Test.txt") If fileExists = True Then Dim fileContents As String 'If the file exists, load its contents into txt_Body fileContents = My.Computer.FileSystem.ReadAllText("C:\Test.txt") txt_Body.Text = fileContents End If 'This API will make the file if its not there, and if it is there, write to it anyway. 'The 'True' at the end is for Append. My.Computer.FileSystem.WriteAllText("TheFile.txt", "TextContents", True)
-
Aug 23rd, 2011, 06:26 PM
#4
Thread Starter
New Member
Re: File handling.
I've tried changing the name of the handle but it still gives the the error saying that the it cannot access a closed file.
-
Aug 23rd, 2011, 06:54 PM
#5
Re: File handling.
Think about it... you can't access a CLOSED file... think about it... think of the file as a box... you can't get to the contents of the box when it's closed, right? So you have to __________ it. (fill in the blank).
-tg
-
Aug 23rd, 2011, 07:08 PM
#6
Thread Starter
New Member
Re: File handling.
I do open the file on the top...
Code:
Dim sample As New FileStream(Application.StartupPath & tb_filename.Text, FileMode.CreateNew, FileAccess.Write, FileShare.ReadWrite)
Somehow the program doesn't detect i do so.
-
Aug 23rd, 2011, 08:35 PM
#7
Re: File handling.
If you're reading and writing text...why not use a StreamWriter & StreamReader directly instead of creating a FileStream first and then using the Writer and Reader?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|