Solved

VB.net create text file used by another process

Posted on 2014-04-15
4
850 Views
Last Modified: 2014-04-17
Hi
I use the following line to create a text file.If I run the application again it says that it is being
used by another process. How do I ensure that this doesn't happen

     oWriter = New System.IO.StreamWriter(SYS_FOLDER & "\Customer_Invoice_Email_Queue.txt")
0
Comment
Question by:murbro
4 Comments
 
LVL 39

Accepted Solution

by:
Pratima Pharande earned 167 total points
Comment Utility
make sure you will close the stremwriter oWriter.Close()




Dim file As System.IO.StreamWriter
file = My.Computer.FileSystem.OpenTextFileWriter("c:\test.txt", True)
file.WriteLine("Here is the first string.")
file.Close()
0
 
LVL 142

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 167 total points
Comment Utility
on top, use USING statement:
http://msdn.microsoft.com/en-us/library/htd05whh.aspx
Using file As System.IO.StreamWriter
file = My.Computer.FileSystem.OpenTextFileWriter("c:\test.txt", True)
file.WriteLine("Here is the first string.")
file.Close() 
End Using 

Open in new window

0
 
LVL 19

Assisted Solution

by:Shahan Ayyub
Shahan Ayyub earned 166 total points
Comment Utility
There are couple of ways to handle it:

1) Use `using` block as suggested by other users
2) Wrap code inside Try..Catch..Finally block, and in finally block `Close` the file.
3) You should wrap it into Try..Catch..Finally block it is possible that the file you are trying to modify is open in another application. So you can not do anything except waiting to release the file by another application.

First two point will help you to handle the case if your application is opening a file and not releasing/closing it. The last point will help if the file is in use by another application.

Hope it helps!
0
 

Author Closing Comment

by:murbro
Comment Utility
Thanks for the help
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

743 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

13 Experts available now in Live!

Get 1:1 Help Now