File Handling in ASP.Net / VB.Net

C

C

Hi,

I have a web app whereby I extract some data from an
Excel Sheet.

Once I have extracted my data I close my Excel Object.

I then try to delete the file.

When I try to delete the file it tells me that it is
locked by the IUSR account.

Anyone know why this is?

Thanks,
C
 
K

Kevin Spencer

A file is locked when it is opened by a process, and remains locked until it
is closed. Apparently, although you didn't mention how, you're opening the
file at some point. How are you opening it? Are you closing it prior to
attempting to delete it?

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
 
D

Dave Davidson

Closing an Excel Application object with COM Interop is
actually a bit tricky. Try code like this:

Private Sub ReleaseCOMObject(ByVal pobj as Object)
Try
System.Runtime.InteropServices.Marshal.ReleaseComObject
(pobj)
Catch
Finally
pobj = Nothing
End Try
End Sub

Private Sub ReadExcelFile()
Dim objApplication As Excel.Application
Dim objWorkbooks As Excel.Workbooks
Dim objWorkbook As Excel.Workbook
Dim strFileName as String
Try
strFileName = "ExcelFile.xls"
objApplication = New Excel.Application
objApplication.DisplayAlerts = False
objWorkbooks = objApplication.Workbooks
objWorkbooks.Open(strFileName)
objWorkbook = objWorkbooks(1)
' Work with data, making sure to use explicitly
' defined objects for each object or collection
' accessed.
' objWorkbook.SaveAs(strFileName & ".htm", 44)
objWorkbook.Close(False)
Catch ex as Exception
' Display error
Finally
ReleaseCOMObject(objWorkbook)
ReleaseCOMObject(objWorkbooks)
Try
objApplication.Quit()
Catch
End Try
ReleaseCOMObject(objApplication)
Try
Kill(strFileName)
Catch ex as Exception
' Display reason why file could not be deleted
End Try
End Try
End Sub


- Dave Davidson (MCP)
 

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

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top