Solved

VB.net create text file used by another process

Posted on 2014-04-15
4
867 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
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.")
file.Close()
0
 
LVL 142

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 167 total points
ID: 40001075
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
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!
0
 

Author Closing Comment

by:murbro
ID: 40006182
Thanks for the help
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
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…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

778 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