• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 581
  • Last Modified:

streamwriter and file is in use issue

Hi,
In my vb.net 2005 project I open a file using   FileOpen(SQLFile, (SQLFileName), OpenMode.Append) or   FileOpen(SQLFile, (SQLFileName), OpenMode.Output) function now when I use the streamwriter
       swLog = New IO.StreamWriter(SQLFileName) it errors saying the file is in use.How can I resolve this without closing the opened file is it possible to amend it ...any suggestions?

Cheers
0
RIAS
Asked:
RIAS
1 Solution
 
Carl TawnSystems and Integration DeveloperCommented:
Try opening it in shared mode:

FileOpen(SQLFile, SQLFileName , Microsoft.VisualBasic.OpenMode.Append, Microsoft.VisualBasic.OpenAccess.Read, Microsoft.VisualBasic.OpenShare.Shared)

Open in new window

0
 
CodeCruiserCommented:
What are you trying to do? Is it a text file?
0
 
RIASAuthor Commented:
Hi,
I am opening a .sql file using
  FileOpen(SQLFile, (SQLFileName), OpenMode.Append) or   FileOpen(SQLFile, (SQLFileName), OpenMode.Output) function.
and using a streamwriter
 swLog = New IO.StreamWriter(SQLFileName, True)
The problem is I need to append one file which is already opened.
This bit of code ---  swLog = New IO.StreamWriter(SQLFileName, True)---falls over if that is the case

Cheers

                 
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
CodeCruiserCommented:
You can use the IO.File.ReadAllText or IO.File.ReadAllLines to read the file. Then you can use the IO.File.AppendAllText to write back to the file.

http://msdn.microsoft.com/en-us/library/ms143368.aspx

http://msdn.microsoft.com/en-us/library/s2tte0y1.aspx

http://msdn.microsoft.com/en-us/library/ms143356.aspx
0
 
RIASAuthor Commented:
Edit to the previous post:

Hi,
I am opening a .sql file using
  using a streamwriter
 swLog = New IO.StreamWriter(SQLFileName1, True)
example abc.sql ---first file
I open a new file  example def.sql ---second file using
swLog = New IO.StreamWriter(SQLFileName2, True)
Now I want to go back to my first file to append it.
The problem is I need to append one file which is already opened.
This bit of code ---  swLog = New IO.StreamWriter(SQLFileName, True)---falls over if that is the case

Cheers

                 
0
 
CodeCruiserCommented:
Try the functions I mentioned above to read and write files.

Also,
>Now I want to go back to my first file to append it.
If you already have a streamwrite open for first file, why are you opening it again?
0
 
RIASAuthor Commented:
How do I close the file?
Cheers
0
 
RIASAuthor Commented:
Hi,
I am not reading data from anywhere I am writing a script with my program so it has all the database values in it.

Cheers
0
 
CodeCruiserCommented:
IF you are just writing then use this

IO.File.AppendAllText("C:\test.txt", "This is some text")

This will append the text to the file or create a file if it does not exist and automatically close the file.
0
 
RIASAuthor Commented:
oki will try cheers..
0
 
RIASAuthor Commented:
Hi,
It did work but what I am trying to do here is enhance the performance speed ..streamwriter gave a great speed but  IO.File.AppendAllText(SQLFile, SQL) or as I used before printline is not as fast as streamwriter.
Any suggestions?

Cheers
0
 
CodeCruiserCommented:
Are you writing to the file in a loop? Or do you write a large text at once?
0
 
RIASAuthor Commented:
In a loop.Retrieving one set of values and then writing ..again retrieving another set of values then writing.
In this situation I have couple of files to create and write..
0
 
CodeCruiserCommented:
You can use a StringBuilder object in the loop and then write the output of string to the file at once after the loop.
0
 
RIASAuthor Commented:
oh ya...very good idea let me implement it.But still want to use streamwriter...as it is tremendously fast compare to other two  IO.File.AppendAllText or printline.Just need to know how to close the file if it in use or open.

Cheers
0
 
CodeCruiserCommented:
Do you dispose the streamwriters after using?
0
 
RIASAuthor Commented:
I close it   by  sw.close ...
0
 
RIASAuthor Commented:
Hi Codecruiser,
Can you guide me on one thing ..how to set a path for a streamwriter
Is this the only method
swLog = New IO.StreamWriter(SQLFileName, True)
Or is there anything like
swLog = path for sqlfile  

Cheers
0
 
RIASAuthor Commented:
Instead of defining it new
swLog = New IO.StreamWriter(SQLFileName, True)
can I change the path

Cheers
0
 
CodeCruiserCommented:
I dont think so. But you can reinitialize it after closing it.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now