VB.net create text file used by another process

Posted on 2014-04-15
Medium Priority
Last Modified: 2014-04-17
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")
Question by:Murray Brown
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 39

Accepted Solution

Pratima Pharande earned 668 total points
ID: 40001050
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.")
LVL 143

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 668 total points
ID: 40001075
on top, use USING statement:
Using file As System.IO.StreamWriter
file = My.Computer.FileSystem.OpenTextFileWriter("c:\test.txt", True)
file.WriteLine("Here is the first string.")
End Using 

Open in new window

LVL 19

Assisted Solution

by:Shahan Ayyub
Shahan Ayyub earned 664 total points
ID: 40003775
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!

Author Closing Comment

by:Murray Brown
ID: 40006182
Thanks for the help

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

Question has a verified solution.

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

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

771 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