HELP: Problem creating/editing file

Discussion in 'ASP .Net' started by VB Programmer, Nov 26, 2003.

  1. I want to write to a simple text file. If it doesn't exist I want to create
    it first. Here is my code (portion):
    Imports System.IO ' at the top of the class
    :
    :
    Dim strFileName As String = "C:\MyFile.txt"

    ' create file if not exist
    If Not File.Exists(strfilename) Then
    File.CreateText(strFileName)

    Dim swStreamWriter As New StreamWriter(strFileName, True)

    With swStreamWriter
    .WriteLine("This is a test. Please append/write to the
    file!!!!!")
    .Close()
    End With

    The file creates fine if it doesn't exist. The problem is that when it gets
    to the Dim for the StreamWriter it gives me this type of error:
    The process cannot access the file "C:\MyFile.txt" because it is being used
    by another process.
    The source is mscorlib.

    Also, when I stop the debug I can't delete the file because it says that a
    "sharing violation" exists and "The source or destination file may be in
    use."

    What am I doing wrong? What is the best way to do this?
    VB Programmer, Nov 26, 2003
    #1
    1. Advertising

  2. VB Programmer

    Cor Guest

    Re: Problem creating/editing file

    Hi VB programmer,

    You want to try this?

    Cor
    \\\
    > Imports System.IO ' at the top of the class
    > :
    > :
    > Dim strFileName As String = "C:\MyFile.txt"

    Dim swStreamWriter As New StreamWriter
    > ' create file if not exist
    > If Not File.Exists(strFileName) Then

    swStreamWriter = File.CreateText(strFileName)
    swStreamWriter.flush
    swStreamWriter.close
    swStreamWriter = File.AppendText(strFileName)
    >

    swStreamWriter.WriteLine("This is a test.")
    swStreamWriter.Flush
    swStreamWriter.Close()
    ///
    Cor
    Cor, Nov 26, 2003
    #2
    1. Advertising

  3. Re: Problem creating/editing file

    Thanks that worked!

    "Cor" <> wrote in message
    news:...
    > Hi VB programmer,
    >
    > You want to try this?
    >
    > Cor
    > \\\
    > > Imports System.IO ' at the top of the class
    > > :
    > > :
    > > Dim strFileName As String = "C:\MyFile.txt"

    > Dim swStreamWriter As New StreamWriter
    > > ' create file if not exist
    > > If Not File.Exists(strFileName) Then

    > swStreamWriter = File.CreateText(strFileName)
    > swStreamWriter.flush
    > swStreamWriter.close
    > swStreamWriter = File.AppendText(strFileName)
    > >

    > swStreamWriter.WriteLine("This is a test.")
    > swStreamWriter.Flush
    > swStreamWriter.Close()
    > ///
    > Cor
    >
    >
    VB Programmer, Nov 26, 2003
    #3
  4. VB Programmer

    Tommy Guest

    It seems like sometimes the file does not get closed properly.
    Try putting your code in a Try/Catch/Finally block. In the Finally
    block, close the stream if the stream object still exist.

    For example,

    finally
    {
    if (swStreamWriter != null)
    {
    swStreamWriter.close();
    }
    }

    Tommy,

    "VB Programmer" <> wrote in message news:<>...
    > I want to write to a simple text file. If it doesn't exist I want to create
    > it first. Here is my code (portion):
    > Imports System.IO ' at the top of the class
    > :
    > :
    > Dim strFileName As String = "C:\MyFile.txt"
    >
    > ' create file if not exist
    > If Not File.Exists(strfilename) Then
    > File.CreateText(strFileName)
    >
    > Dim swStreamWriter As New StreamWriter(strFileName, True)
    >
    > With swStreamWriter
    > .WriteLine("This is a test. Please append/write to the
    > file!!!!!")
    > .Close()
    > End With
    >
    > The file creates fine if it doesn't exist. The problem is that when it gets
    > to the Dim for the StreamWriter it gives me this type of error:
    > The process cannot access the file "C:\MyFile.txt" because it is being used
    > by another process.
    > The source is mscorlib.
    >
    > Also, when I stop the debug I can't delete the file because it says that a
    > "sharing violation" exists and "The source or destination file may be in
    > use."
    >
    > What am I doing wrong? What is the best way to do this?
    Tommy, Nov 27, 2003
    #4
  5. Re: Problem creating/editing file

    Hello, Cor:

    I think the problem was that the line "If Not File.Exists(strfilename) Then File.CreateText(strFileName)" created a StreamWriter object that is never closed (in the code) so the file is in use until the GC closes it, some time after the procedure ends, don't you think so?
    In that case, the swStreamWriter.flush calls shouldn't be necessary. Am I right?

    Regards.
    =?Windows-1252?Q?Jos=E9_Manuel_Ag=FCero?=, Nov 28, 2003
    #5
  6. VB Programmer

    Cor Guest

    Re: Problem creating/editing file

    Hi Jose,

    No there where at least two errors, the close and the open from the file,
    that was without an append.

    About that flush I think that you are right but because that error with the
    close of the full disk last time I added it, dont know why, something tells
    me now that it is stupid thinking so, and that where not the errors.

    Cor

    >I think the problem was that the line "If Not File.Exists(strfilename) Then
    >File.CreateText(strFileName)" created a StreamWriter object that is never
    >closed (in the code) so the file is in use until the GC closes it, some

    time >after the procedure ends, don't you think so?
    >In that case, the swStreamWriter.flush calls shouldn't be necessary. Am I
    >right?
    Cor, Nov 28, 2003
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jerry Khoo

    help on file editing

    Jerry Khoo, Jun 17, 2004, in forum: C++
    Replies:
    4
    Views:
    410
    Thomas Matthews
    Jun 17, 2004
  2. Sree

    PROBLEM when editing DAT file!

    Sree, Jan 23, 2006, in forum: C Programming
    Replies:
    2
    Views:
    276
    Keith Thompson
    Jan 23, 2006
  3. Replies:
    0
    Views:
    572
  4. Steve555
    Replies:
    2
    Views:
    278
    Steve555
    Mar 20, 2007
  5. Sooraj S
    Replies:
    2
    Views:
    388
    Dietmar Kuehl
    Jan 19, 2011
Loading...

Share This Page