File Close problem

B

Bruce Wiebe

hi all

im having a problem accessing a text file on my hard disk after ive
created it and added some text to it it would appear that the file is
still locked.

What happens is this i have three buttons on a windows form that provide
three options for the user the process goes like this

user clicks button one and a web service is called that dwnloads a list
of products to a local dataset, this is then looped through to extract a
product id and calls a function that then retrieves values for that
product this part seems to work fine and i end up with a lovelly tab
delimited file on my hard drive. however when i click the second button
(which is supposed to zip the file up) the system tells me that it cant
access the file as it is in use by another process i have scoured the
group for answers but to no avail and any help would be much appreciated
as thias is nearly the last part of this project.

Below i have placed the code for the two button click events and the
function that is called byt the button click any suggectiosn would be
useful ignore the scrappy code as i havnt tidied it up yet tend to do
that once its finished ;-)

i hope someone can help as im pulling my hair out over this

Code for first button click event

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim ws As New localhost.webservice

'write the header into the file
'Dim objstreamwriter As StreamWriter
'objstreamwriter = File.CreateText(feedname)
Dim fileInfo As New FileInfo(feedname)
Dim s As StreamWriter = fileInfo.CreateText()
s.WriteLine("product_url" & Chr(9) & "name" & Chr(9) &
"description" & Chr(9) & "price" & Chr(9) & "Image_url" & Chr(9) &
"category" & Chr(9) & "offer_id")
s.Close()

Dim localds As DataSet
Dim row As DataRow
Dim thistable As DataTable
Dim count As Integer
Dim productid As String
Dim mycolumn As DataColumn

localds = ws.Getproducts("passwordgoeshere", "passwordgoeshere")

Dim totalrows As String = localds.Tables(0).Rows.Count

For Each thistable In localds.Tables
' For each row, print the values of each column.
Dim myRow As DataRow
count = 0
For Each myRow In thistable.Rows
productid = myRow("productid")
getinfo(productid)
count = count + 1
StatusBarPanel1.Text = "Processing Record Number " &
count & " of " & totalrows
Next myRow
Next thistable

StatusBar1.Text = "Feed File Created Succesfully"
MsgBox("Feed Has Been Created", MsgBoxStyle.Information,
"Information")
Button2.Enabled = True

End Sub

Code for getinfo function

Public Function getinfo(ByVal productid As String)
'call a webservice to get the details depending on the productid
Dim ws As New localhost.webservice

'get the exchange rate setting from the database
Dim exchangerate As String =
ws.getExchangeRates("passwordhoeshere", "passwordhoeshere")

Dim productname As String
Dim pname As String = ws.getproductname("passwordhoeshere",
"passwordhoeshere", productid)
Dim url As String
Dim descriptionin As String =
ws.getProductDescription("passwordhoeshere", "passwordhoeshere", productid)
Dim descriptionout As String
Dim descriptionout1 As String
Dim desc As String
Dim price As String = ws.getProductPrice("passwordhoeshere",
"passwordhoeshere", productid)
Dim imageurl As String
Dim category As String = ws.getCategoryid("passwordhoeshere",
"passwordhoeshere", productid)
Dim offerid As String
Dim imagename As String =
ws.getProductimage("passwordhoeshere", "passwordhoeshere", productid)

Dim mastercategory As String
Dim subcategory As String
Dim categoryid As String
Dim categorystring As String
Dim rate As Decimal = ws.getExchangeRates("passwordhoeshere",
"passwordhoeshere")

descriptionout1 = Regex.Replace(descriptionin, "\s+", " ")

desc = Regex.Replace(descriptionout1, "<[^>]*>", "")

If Len(desc) > 1000 Then
descriptionout = Microsoft.VisualBasic.Left(desc, 1000)
Else
descriptionout = desc
End If

If Len(pname) > 80 Then
productname = Microsoft.VisualBasic.Left(pname, 80)
Else
productname = pname
End If

'check categorys for length and get category names etc

If Len(category) = 8 Or Len(category) > 8 Then

categoryid = ws.getSubCategoryid("passwordhoeshere",
"passwordhoeshere", category)
subcategory = ws.getSubCategoryName("passwordhoeshere",
"passwordhoeshere", category)

'now we get the master category
mastercategory = ws.getmastercategory("passwordhoeshere",
"passwordhoeshere", categoryid)


'create the string that will be sued in the file
categorystring = mastercategory & ">" & subcategory
Else
mastercategory = ws.getmastercategory("passwordhoeshere",
"passwordhoeshere", category)
categorystring = mastercategory
End If

Dim totalprice As String

totalprice = String.Format("{0:n}", CDec(price) * CDec(rate))

'create the imageurl that will go in the file needs changing in
the final version
imageurl = siteroot & "/productimages/" & imagename
url = ws.getRootURL("passwordhoeshere", "passwordhoeshere") &
"/productdetails.aspx?productid=" & productid

'append the values into the file
Dim objstreamwriter As StreamWriter

objstreamwriter = File.AppendText(feedname)
objstreamwriter.WriteLine(url & Chr(9) & productname & Chr(9) &
descriptionout & Chr(9) & totalprice & Chr(9) & imageurl & Chr(9) &
categorystring & Chr(9))

objstreamwriter.Close()



End Function

'code for zipping up file
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
'zip up the file with the supplied feedname

Dim objCrc32 As New Crc32
Dim strmZipOutputStream As ZipOutputStream

strmZipOutputStream = New ZipOutputStream(File.Create(feedname))
strmZipOutputStream.SetLevel(9)

REM Compression Level: 0-9
REM 0: no(Compression)
REM 9: maximum compression

Dim strFile As String = (feedname)

'For Each strFile In astrFileNames
Dim strmFile As FileStream = File.OpenRead(strFile)
Dim abyBuffer(strmFile.Length - 1) As Byte

strmFile.Read(abyBuffer, 0, abyBuffer.Length)
Dim objZipEntry As ZipEntry = New ZipEntry(strFile)

objZipEntry.DateTime = DateTime.Now
objZipEntry.Size = strmFile.Length
strmFile.Close()
objCrc32.Reset()
objCrc32.Update(abyBuffer)
objZipEntry.Crc = objCrc32.Value
strmZipOutputStream.PutNextEntry(objZipEntry)
strmZipOutputStream.Write(abyBuffer, 0, abyBuffer.Length)

'Next

strmZipOutputStream.Finish()
strmZipOutputStream.Close()

MsgBox("Feed File has been Zipped Sucessfully",
MsgBoxStyle.Information, "Information")
Button3.Enabled = True

End Sub
 
?

=?iso-8859-1?Q?Jos=E9_Manuel_Ag=FCero?=

Hello, Bruce:

It seems tha the files are not freed until the GC removes the references.
Try adding GC.Collect after each file.Close() statement.
This is not good practice (it hurts the performance), so if it doesn't solve your problem, remove the GC.Collect statements.

Regards.


"Bruce Wiebe" <[email protected]> escribió en el mensaje | hi all
|
| im having a problem accessing a text file on my hard disk after ive
| created it and added some text to it it would appear that the file is
| still locked.
|
|...
|
 
A

Alireza Kheyrollahi

You should always call Close on connections and streams. One of the funny
things is that when you use streamreader or stream writer you still should
call close on the stream in addition to the reader or writer (they were
supposed to call it themselves but the other day I found that if I do not
call it, it stays open!)

Sometimes if the file is selected in windows explorer, it is locked! Another
mystery of the great features of the windows explorer!

Cheers
Ali Kheyrollahi


Bruce Wiebe said:
hi all

im having a problem accessing a text file on my hard disk after ive
created it and added some text to it it would appear that the file is
still locked.

What happens is this i have three buttons on a windows form that provide
three options for the user the process goes like this

user clicks button one and a web service is called that dwnloads a list
of products to a local dataset, this is then looped through to extract a
product id and calls a function that then retrieves values for that
product this part seems to work fine and i end up with a lovelly tab
delimited file on my hard drive. however when i click the second button
(which is supposed to zip the file up) the system tells me that it cant
access the file as it is in use by another process i have scoured the
group for answers but to no avail and any help would be much appreciated
as thias is nearly the last part of this project.

Below i have placed the code for the two button click events and the
function that is called byt the button click any suggectiosn would be
useful ignore the scrappy code as i havnt tidied it up yet tend to do
that once its finished ;-)

i hope someone can help as im pulling my hair out over this

Code for first button click event

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim ws As New localhost.webservice

'write the header into the file
'Dim objstreamwriter As StreamWriter
'objstreamwriter = File.CreateText(feedname)
Dim fileInfo As New FileInfo(feedname)
Dim s As StreamWriter = fileInfo.CreateText()
s.WriteLine("product_url" & Chr(9) & "name" & Chr(9) &
"description" & Chr(9) & "price" & Chr(9) & "Image_url" & Chr(9) &
"category" & Chr(9) & "offer_id")
s.Close()

Dim localds As DataSet
Dim row As DataRow
Dim thistable As DataTable
Dim count As Integer
Dim productid As String
Dim mycolumn As DataColumn

localds = ws.Getproducts("passwordgoeshere", "passwordgoeshere")

Dim totalrows As String = localds.Tables(0).Rows.Count

For Each thistable In localds.Tables
' For each row, print the values of each column.
Dim myRow As DataRow
count = 0
For Each myRow In thistable.Rows
productid = myRow("productid")
getinfo(productid)
count = count + 1
StatusBarPanel1.Text = "Processing Record Number " &
count & " of " & totalrows
Next myRow
Next thistable

StatusBar1.Text = "Feed File Created Succesfully"
MsgBox("Feed Has Been Created", MsgBoxStyle.Information,
"Information")
Button2.Enabled = True

End Sub

Code for getinfo function

Public Function getinfo(ByVal productid As String)
'call a webservice to get the details depending on the productid
Dim ws As New localhost.webservice

'get the exchange rate setting from the database
Dim exchangerate As String =
ws.getExchangeRates("passwordhoeshere", "passwordhoeshere")

Dim productname As String
Dim pname As String = ws.getproductname("passwordhoeshere",
"passwordhoeshere", productid)
Dim url As String
Dim descriptionin As String =
ws.getProductDescription("passwordhoeshere", "passwordhoeshere", productid)
Dim descriptionout As String
Dim descriptionout1 As String
Dim desc As String
Dim price As String = ws.getProductPrice("passwordhoeshere",
"passwordhoeshere", productid)
Dim imageurl As String
Dim category As String = ws.getCategoryid("passwordhoeshere",
"passwordhoeshere", productid)
Dim offerid As String
Dim imagename As String =
ws.getProductimage("passwordhoeshere", "passwordhoeshere", productid)

Dim mastercategory As String
Dim subcategory As String
Dim categoryid As String
Dim categorystring As String
Dim rate As Decimal = ws.getExchangeRates("passwordhoeshere",
"passwordhoeshere")

descriptionout1 = Regex.Replace(descriptionin, "\s+", " ")

desc = Regex.Replace(descriptionout1, "<[^>]*>", "")

If Len(desc) > 1000 Then
descriptionout = Microsoft.VisualBasic.Left(desc, 1000)
Else
descriptionout = desc
End If

If Len(pname) > 80 Then
productname = Microsoft.VisualBasic.Left(pname, 80)
Else
productname = pname
End If

'check categorys for length and get category names etc

If Len(category) = 8 Or Len(category) > 8 Then

categoryid = ws.getSubCategoryid("passwordhoeshere",
"passwordhoeshere", category)
subcategory = ws.getSubCategoryName("passwordhoeshere",
"passwordhoeshere", category)

'now we get the master category
mastercategory = ws.getmastercategory("passwordhoeshere",
"passwordhoeshere", categoryid)


'create the string that will be sued in the file
categorystring = mastercategory & ">" & subcategory
Else
mastercategory = ws.getmastercategory("passwordhoeshere",
"passwordhoeshere", category)
categorystring = mastercategory
End If

Dim totalprice As String

totalprice = String.Format("{0:n}", CDec(price) * CDec(rate))

'create the imageurl that will go in the file needs changing in
the final version
imageurl = siteroot & "/productimages/" & imagename
url = ws.getRootURL("passwordhoeshere", "passwordhoeshere") &
"/productdetails.aspx?productid=" & productid

'append the values into the file
Dim objstreamwriter As StreamWriter

objstreamwriter = File.AppendText(feedname)
objstreamwriter.WriteLine(url & Chr(9) & productname & Chr(9) &
descriptionout & Chr(9) & totalprice & Chr(9) & imageurl & Chr(9) &
categorystring & Chr(9))

objstreamwriter.Close()



End Function

'code for zipping up file
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
'zip up the file with the supplied feedname

Dim objCrc32 As New Crc32
Dim strmZipOutputStream As ZipOutputStream

strmZipOutputStream = New ZipOutputStream(File.Create(feedname))
strmZipOutputStream.SetLevel(9)

REM Compression Level: 0-9
REM 0: no(Compression)
REM 9: maximum compression

Dim strFile As String = (feedname)

'For Each strFile In astrFileNames
Dim strmFile As FileStream = File.OpenRead(strFile)
Dim abyBuffer(strmFile.Length - 1) As Byte

strmFile.Read(abyBuffer, 0, abyBuffer.Length)
Dim objZipEntry As ZipEntry = New ZipEntry(strFile)

objZipEntry.DateTime = DateTime.Now
objZipEntry.Size = strmFile.Length
strmFile.Close()
objCrc32.Reset()
objCrc32.Update(abyBuffer)
objZipEntry.Crc = objCrc32.Value
strmZipOutputStream.PutNextEntry(objZipEntry)
strmZipOutputStream.Write(abyBuffer, 0, abyBuffer.Length)

'Next

strmZipOutputStream.Finish()
strmZipOutputStream.Close()

MsgBox("Feed File has been Zipped Sucessfully",
MsgBoxStyle.Information, "Information")
Button3.Enabled = True

End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top