Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Visual Basic > VB 2008 > Visual Basic 2008 Essentials
|
Visual Basic 2008 Essentials If you are new to Visual Basic programming with version 2008, this is the place to start your questions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Visual Basic 2008 Essentials section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 14th, 2009, 12:50 PM
Registered User
 
Join Date: May 2009
Posts: 2
Thanks: 2
Thanked 0 Times in 0 Posts
Default Need to release file after File.Create...

Thanks in Advance.

I need to create files in a Sub and immediately write from StreamWriter. I can't figure out how to release the newly created files so StreamWriter can work. I get an error that file is in use by another process. This works if I create the files outside of VB and just run the StreamWriter.

Code:
 
Imports System.IO
Public Class Form1
    Public a_HoldArray(60) As String  'a_HoldArray is populated in another Sub.
    Public filenamevariable As String
 
    'CREATE AND WRITE FILE
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        ' GET A VARIABLE INTO THE FILE NAME
        filenamevariable = "DataTime"
        File.Create("C:\Documents and Settings\xxxxxx_" & filenamevariable & "_xxxxxx.txt")
 
        Dim FileWriter As StreamWriter
        FileWriter = New StreamWriter("C:\Documents and Settings\xxxxxx_DateTime_xxxxxx.txt", False)
        For i = 1 To UBound(a_HoldArray)
            FileWriter.WriteLine(a_HoldArray(i))
        Next
        FileWriter.Close()
    End Sub
End Class
 
Old May 14th, 2009, 01:09 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

You never try google..

Msdn has this:

The FileStream object created by this method has a default FileShare value of None; no other process or code can access the created file until the original file handle is closed.

Since you never release the file, it's still open...

http://msdn.microsoft.com/en-us/library/d62kzs03.aspx
__________________
HTH

Gonzalo


================================================== =========
Read this if you want to know how to get a correct reply for your question.
(Took that from Doug signature and he Took that from Peter profile)
================================================== =========
My programs achieved a new certification :
WORKS ON MY MACHINE
================================================== =========
I know that CVS was evil, and now i got the
proof.
================================================== =========
The Following User Says Thank You to gbianchi For This Useful Post:
VBtyler (May 14th, 2009)
 
Old May 19th, 2009, 07:12 AM
Friend of Wrox
 
Join Date: Jun 2005
Posts: 244
Thanks: 3
Thanked 4 Times in 4 Posts
Default

Having said that though, i've got the same problem even when i release all the objects from memory even though the streamWriter and streamReader method writes it all to disk after you close it!

Mind you, having said all that i haven't really looked on msdn so i will have to migrate my url to msdn for quick lookup!
__________________
Apocolypse2005, I'm a programmer - of sorts.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Please help: Can't create file Rachel BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 0 January 10th, 2009 11:56 AM
How to create a Release configuration? dililili ASP.NET 2.0 Professional 0 November 2nd, 2006 11:33 PM
How to create a Release configuration? dililili ASP.NET 2.0 Basics 0 November 2nd, 2006 03:54 PM
File.Create C# everest General .NET 1 September 6th, 2005 07:48 PM
How to create exe file. ? abdusalam VB How-To 3 July 5th, 2004 02:05 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.