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? 5 1157
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
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
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?
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.
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? This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 ?
|
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...
|
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.
|
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...
|
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)
| |
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.
|
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...
|
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
|
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
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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...
| |
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...
|
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...
|
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...
|
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...
|
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: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |