Results 1 to 7 of 7

Thread: File handling.

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2011
    Posts
    3

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

  2. #2
    Fanatic Member
    Join Date
    Nov 2007
    Posts
    520

    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:
    1. 'Code Removed
    2. '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.

  3. #3
    Fanatic Member
    Join Date
    Nov 2007
    Posts
    520

    Re: File handling.

    Modified the code...

    vb Code:
    1. 'Determins if the file exists...
    2.         Dim fileExists As Boolean = fileExists = My.Computer.FileSystem.FileExists("C:\Test.txt")
    3.         If fileExists = True Then
    4.             Dim fileContents As String
    5.             'If the file exists, load its contents into txt_Body
    6.             fileContents = My.Computer.FileSystem.ReadAllText("C:\Test.txt")
    7.             txt_Body.Text = fileContents
    8.         End If
    9.  
    10.         'This API will make the file if its not there, and if it is there, write to it anyway.
    11.         'The 'True' at the end is for Append.
    12.         My.Computer.FileSystem.WriteAllText("TheFile.txt", "TextContents", True)

  4. #4

    Thread Starter
    New Member
    Join Date
    Aug 2011
    Posts
    3

    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.

  5. #5
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  6. #6

    Thread Starter
    New Member
    Join Date
    Aug 2011
    Posts
    3

    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.

  7. #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
  •  



Click Here to Expand Forum to Full Width