473,804 Members | 3,478 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(str filename) Then
File.CreateText (strFileName)

Dim swStreamWriter As New StreamWriter(st rFileName, True)

With swStreamWriter
.WriteLine("Thi s 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 20 '05 #1
5 1467
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(str FileName) 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 20 '05 #2
Thanks that worked!

"Cor" <no*@non.com> wrote in message
news:ur******** ******@tk2msftn gp13.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(str FileName) 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 20 '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******* *******@TK2MSFT NGP10.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(str filename) Then
File.CreateText (strFileName)

Dim swStreamWriter As New StreamWriter(st rFileName, True)

With swStreamWriter
.WriteLine("Thi s 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 20 '05 #4
Hello, Cor:

I think the problem was that the line "If Not File.Exists(str filename) 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 20 '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(str filename) Then
File.CreateTex t(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 20 '05 #6

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

Similar topics

2
3561
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 lexPOV.cxx file in the package, but I can not find any POV keywords in any file ( there are about 150 POV keywords). Did i miss it, and if not, how do I create one and include it for the library building ?
9
1402
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 with data stored in an XML document. Basic functionality is required such as viewing, searching, editing, creating and deleting entries for things such as paper submission deadlines, events, funding application deadlines. I would also like to use...
2
1789
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 the editing instructions, and a file that will contain the result of the editing.
5
1164
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 = "C:\MyFile.txt" ' create file if not exist If Not File.Exists(strfilename) Then File.CreateText(strFileName)
0
940
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 the connection, the adapter, the dataset all in the aspx.cs file. I also make the oledb command there, and it works fine...until I try to edit it. The datagrid I make uses the tool under webforms in the aspx file. I think its because everything...
4
2218
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 wanted to move my work and needed to place it on the server. Using VS 2005 , went to BUILD -Publish Web Site Checked the box for :: Alow this precompiled site to be updatable.
4
1908
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 issue at hand is to create an accessible editor for open office. there are a lot of things remaining on that front. so right now I am trying to find out a temporary work around by creating a simple accessible editor (with wxpython) for viewing and...
1
1159
by: sowmram | last post by:
Hi, I'm trying to create a site very similar to wiki, where you can edit, save.. Generally update data for the future use... This particular program I wanna do it using javascripts... Please help me out in getting into the solution for the same... Sample prg which I hve done is as below... Looking forward for all of your help...Thanks in advance
2
3826
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
2665
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 need to read in a text file... shown below H H,E,L E,B,F B,A,C A,null,null
0
10593
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10340
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10329
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9163
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7626
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6858
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5527
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5663
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4304
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.