File still in use after StreamReader .Close()
File still in use after StreamReader .Close()
File still in use after StreamReader .Close()
File still in use after StreamReader .Close()
File still in use after StreamReader .Close()
File still in use after StreamReader .Close() File still in use after StreamReader .Close() File still in use after StreamReader .Close() File still in use after StreamReader .Close() File still in use after StreamReader .Close() File still in use after StreamReader .Close() File still in use after StreamReader .Close() File still in use after StreamReader .Close()
File still in use after StreamReader .Close() File still in use after StreamReader .Close()
File still in use after StreamReader .Close()
Go Back  Xtreme Visual Basic Talk > > > File still in use after StreamReader .Close()


Reply
 
Thread Tools Display Modes
  #1  
Old 12-17-2003, 08:14 PM
VolcanoMan VolcanoMan is offline
Newcomer
 
Join Date: Dec 2003
Posts: 1
Question File still in use after StreamReader .Close()


Dear All,

I have written the below codes to open a file, looping it until the end of file, read the content and close the file. I have issued a StreamReader.Close() at the end of the Do...Loop. I even set the StreamReader to Nothing. However, when I wanted to remove it, the File.Delete could not be succeeded. It returned an IOException as the file was still in use by another program. Could anyone please help me out with this? Actually, I have written another function which performs the similar file opening and deleting function. It works fine without problem. I am extremely puzzled. Millions of thanks in advance!

Code:
Dim SrReadLine As StreamReader = New StreamReader(FName, System.Text.Encoding.Default) Do Dim ArrayCount, ClauseLen, intResult As Integer Dim Clause, strSQL As String Dim strPolNo As String Dim LineNo(200) As String Clause = "" strSQL = "" strPolNo = "" For intCount = 1 To ColCount LineNo(intCount) = szSrcLine Clause = Clause & LineNo(intCount) szSrcLine = SrReadLine.ReadLine() Next If ColCount = 6 Then strSQL = "INSERT INTO LOC (ContractNo, Line1, Line2, Line3, Line4, Line5, Line6) " & _ "VALUES ('"strPolNo & "', " & Clause & ")" Else strSQL = "INSERT INTO LTA (ContractNo, Line1, Line2, Line3, Line4, Line5, Line6, Line7) " & _ "VALUES ('" strPolNo & "', " & Clause & ")" End If objCmd.CommandText = strSQL intResult = objCmd.ExecuteNonQuery() SrReadLine.BaseStream.Flush() Loop Until IsNothing(szSrcLine) SrReadLine.Close() SrReadLine = Nothing objCmd.Cancel()

VolcanoMan

Last edited by reboot; 12-17-2003 at 10:03 PM. Reason: Added vb tags
Reply With Quote
  #2  
Old 12-18-2003, 11:12 AM
Csharp's Avatar
CsharpFile still in use after StreamReader .Close() Csharp is offline
Senior Contributor

* Expert *
 
Join Date: Jul 2003
Location: Ashby, Leicestershire.
Posts: 967
Default

you should be using the Peek() property , not a Do Loop , here's a quick example i knocked up for you...
Code:
Dim sReader As New StreamReader(New FileStream("C:\sample.txt", FileMode.Open)) Dim szSrcLine As String Dim x As Integer = 0 While Not sReader.Peek = -1 szSrcLine = sReader.ReadLine x += 1 '/// increase by 1 each time a line is read. If x = 6 Then '/// do stuff Else '/// do other stuff End If End While sReader.Close() File.Delete("C:\sample.txt")
__________________
~~ please don't PM me regarding code, I only reply to personnal messages ~~
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
File I/O GavinO Tutors' Corner 5 04-17-2013 02:18 PM
Installation Problem - PLs help urgenlty dpdsouza Installation / Documentation 4 12-02-2004 08:09 PM
Doesn't want to register! MikeyM Installation / Documentation 5 03-02-2003 09:22 PM

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
File still in use after StreamReader .Close()
File still in use after StreamReader .Close()
File still in use after StreamReader .Close() File still in use after StreamReader .Close()
File still in use after StreamReader .Close()
File still in use after StreamReader .Close()
File still in use after StreamReader .Close() File still in use after StreamReader .Close() File still in use after StreamReader .Close() File still in use after StreamReader .Close() File still in use after StreamReader .Close() File still in use after StreamReader .Close() File still in use after StreamReader .Close()
File still in use after StreamReader .Close()
File still in use after StreamReader .Close()
 
File still in use after StreamReader .Close()
File still in use after StreamReader .Close()
 
-->