473,385 Members | 1,824 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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?
Nov 18 '05 #1
5 1143
Cor
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
Nov 18 '05 #2
Thanks that worked!

"Cor" <no*@non.com> wrote in message
news:ur**************@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

Nov 18 '05 #3
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" <gr*********@go-intech.com> wrote in message news:<OJ**************@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?

Nov 18 '05 #4
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.

Nov 18 '05 #5
Cor
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?


Nov 18 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: fabien | last post by:
Hi, I am writing a POV-RAY editor with Python using either QT or GTK as GUI 'wrapper'. ( I am still trying both ) * * * * PYGTK * * * * I have downloaded PygtkScintilla-1.99.5. There is a...
9
by: Rob Cowie | last post by:
Hi, This is my first post so go easy! I have been asked (as part of an MSc project) to create a server based planner for a research group at my uni. It will have a web interface to interact...
2
by: Parker.Jim | last post by:
I need to write a program which performs word subsitutions on a text file. The program should input the names of three text files: the source file that will be "edited", a text file that contains...
0
by: sJeev via DotNetMonster.com | last post by:
Please help, I am very new to asp.net and data grids have got me stuck for a loooong time. I can either have the right content in my datagrids or make them editable. In the first case, I make...
5
by: VB Programmer | last post by:
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 =...
4
by: Brad Isaacs | last post by:
I am working with ASP.NET 2.0 and using an SQL Server 2000 database. I am using Visual Studio 2005 and developing on my Local machine. I am working with Login controls ASP.Configuration, I...
4
by: krishnakant Mane | last post by:
hello, right now I am involved on doing a very important accessibility work. as many people may or may not know that I am a visually handicap person and work a lot on accessibility. the main...
2
by: slizorn | last post by:
hi guys, i need to make a tree traversal algorithm that would help me search the tree.. basically i need to read in a text file... shown below H H,E,L E,B,F B,A,C A,null,null c,null,D
2
by: slizorn | last post by:
hi guys, i need to make a tree traversal algorithm that would help me search the tree.. creating a method to search a tree to find the position of node and to return its pointer value basically i...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.