|
-
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!...
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
|