473,287 Members | 1,492 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,287 software developers and data experts.

File Close problem

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

Nov 20 '05 #1
2 1877
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" <Br***@wolfnetwork.net> escribió en el mensaje news:bo**********@sparta.btinternet.com...
| 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.
|
|...
|
Nov 20 '05 #2
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" <Br***@wolfnetwork.net> wrote in message
news:bo**********@sparta.btinternet.com...
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

Nov 20 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Abhas | last post by:
> > Hi, this is Abhas, > > I had made a video library program in C++, but was facing a problem. > > After entering 12 movies, i cannot enter any more movies. > > Something gibberish comes instead....
7
by: Anbu | last post by:
Hi, I want to load contents of a remote HTML file and to display it on the ASP ..NET Web Page. The remote HTML file is not under any virtual folder and the content & location may vary for each...
18
by: Jen | last post by:
I'm using Microsoft's own VB.NET FTP Example: http://support.microsoft.com/default.aspx?scid=kb;en-us;832679 I can get the program to create directories, change directories, etc., but I can't...
34
by: Ross Reyes | last post by:
HI - Sorry for maybe a too simple a question but I googled and also checked my reference O'Reilly Learning Python book and I did not find a satisfactory answer. When I use readlines, what...
4
by: Matt Jensen | last post by:
Howdy I've got a rather strange issue occuring. I used forms based .NET authentication, although I'm also setting some session variables when people login. However, I've found when people use...
18
by: panig | last post by:
how the program knows when a file is finsihed, mmm?
3
by: Yang | last post by:
Hi, I'm experiencing a problem when trying to close the file descriptor for a socket, creating another socket, and then closing the file descriptor for that second socket. I can't tell if my issue...
0
by: Filemaxor | last post by:
I have gotten my code to be able to allow people to add new text to a .txt document and able to call up files so the user can see whats in it. The problem i'm having is getting the for loop to work...
3
by: =?Utf-8?B?TG9yZW4=?= | last post by:
I’m trying to encrypt and decrypt a file in vb.net. I am using the TripleDESCryptoServiceProvider encryption found in System.Security.Cryptography. Below is the code for my Encrypt and Decrypt...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.