Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   ASP .Net (http://www.velocityreviews.com/forums/f29-asp-net.html)
-   -   HELP: Problem creating/editing file (http://www.velocityreviews.com/forums/t67626-help-problem-creating-editing-file.html)

VB Programmer 11-26-2003 02:20 PM

HELP: Problem creating/editing file
 
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?



Cor 11-26-2003 03:04 PM

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



VB Programmer 11-26-2003 03:39 PM

Re: Problem creating/editing file
 
Thanks that worked!

"Cor" <non@non.com> wrote in message
news:uroNF7CtDHA.2304@tk2msftngp13.phx.gbl...
> 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
>
>




Tommy 11-27-2003 04:33 PM

Re: HELP: Problem creating/editing file
 
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" <growNO-SPAM@go-intech.com> wrote in message news:<OJCZNiCtDHA.2208@TK2MSFTNGP10.phx.gbl>...
> 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?


=?Windows-1252?Q?Jos=E9_Manuel_Ag=FCero?= 11-28-2003 05:36 PM

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.


Cor 11-28-2003 07:48 PM

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?






All times are GMT. The time now is 05:18 AM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57