Solved

streamwriter and file is in use issue

Posted on 2010-11-16
21
539 Views
Last Modified: 2012-05-10
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
Comment
Question by:RIAS
21 Comments
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 34147561
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
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 34150410
What are you trying to do? Is it a text file?
0
 

Author Comment

by:RIAS
ID: 34153558
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
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 34153603
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
 

Author Comment

by:RIAS
ID: 34153639
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
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 34153669
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
 

Author Comment

by:RIAS
ID: 34153868
How do I close the file?
Cheers
0
 

Author Comment

by:RIAS
ID: 34153870
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
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 500 total points
ID: 34153947
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
 

Author Comment

by:RIAS
ID: 34154071
oki will try cheers..
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

Author Comment

by:RIAS
ID: 34154450
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
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 34154769
Are you writing to the file in a loop? Or do you write a large text at once?
0
 

Author Comment

by:RIAS
ID: 34155009
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
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 34155088
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
 

Author Comment

by:RIAS
ID: 34155523
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
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 34155624
Do you dispose the streamwriters after using?
0
 

Author Comment

by:RIAS
ID: 34155813
I close it   by  sw.close ...
0
 

Author Comment

by:RIAS
ID: 34155934
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
 
LVL 10

Expert Comment

by:GlobaLevel
ID: 34155964
0
 

Author Comment

by:RIAS
ID: 34155986
Instead of defining it new
swLog = New IO.StreamWriter(SQLFileName, True)
can I change the path

Cheers
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 34158173
I dont think so. But you can reinitialize it after closing it.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Help with Query not working in client's PC 1 35
vb.net - How to check if current user is an administrator? 6 34
Class object 2 26
Variable Event ? 3 19
Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…
Delivering innovative fully-managed cloud services for mission-critical applications requires expertise in multiple areas plus vision and commitment. Meet a few of the people behind the quality services of Concerto.

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now