Help with Deleting a file

  • Thread starter Thread starter cmdolcet69
  • Start date Start date
C

cmdolcet69

I have the below code that calls a function to delete a file that i
added to a listview node. What happens is that that file doesn;t
delete. Please help me out. I have included the event handler and
function. Thanks


Private Sub btnDeleteReport_Click(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles btnDeleteReport.Click


_partfile.Reports.RemoveReport(Me.tvAddedReports.Nodes(0).Nodes(0).Text,
Me.tvAddedReports.Nodes(0).Nodes(0).Nodes(0).Text)

End Sub
----------------------------------------------------------------------------------------------------------------------------------------------------------------------


Public Function RemoveReport(ByVal rPath As String, ByVal rPrinter As
String) As Integer
Try
Dim intloop As Integer
For intloop = 0 To reportArray.Count - 1
If CType(reportArray(intloop), Reports).ReportPath =
rPath And CType(reportArray(intloop), Reports).ReportPrinter =
rPrinter Then
reportArray.RemoveAt(intloop)
Exit For
End If
Next
Return 0
Catch ex As Exception
Return -1
End Try
End Function
 
I have the below code that calls a function to delete a file that i
added to a listview node. What happens is that that file doesn;t
delete. Please help me out. I have included the event handler and
function. Thanks

Private Sub btnDeleteReport_Click(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles btnDeleteReport.Click

_partfile.Reports.RemoveReport(Me.tvAddedReports.Nodes(0).Nodes(0).Text,
Me.tvAddedReports.Nodes(0).Nodes(0).Nodes(0).Text)

End Sub
---------------------------------------------------------------------------­---------------------------------------------------------------------------­----------------

Public Function RemoveReport(ByVal rPath As String, ByVal rPrinter As
String) As Integer
Try
Dim intloop As Integer
For intloop = 0 To reportArray.Count - 1
If CType(reportArray(intloop), Reports).ReportPath =
rPath And CType(reportArray(intloop), Reports).ReportPrinter =
rPrinter Then
reportArray.RemoveAt(intloop)
Exit For
End If
Next
Return 0
Catch ex As Exception
Return -1
End Try
End Function

It appears to me that it only removes the entry for the file in the
list view. To actually delete the file I believe you will need to do
a:

System.IO.File.Delete("path to file")
 
It appears to me that it only removes the entry for the file in the
list view. To actually delete the file I believe you will need to do
a:

System.IO.File.Delete("path to file")
 
---------------------------------------------------------------------------­-----------------
za how can I call this function in the event handler?

I thank you so much- Hide quoted text -

- Show quoted text -

================================================================================
Do just what he says - that's all you need.
Or you can use the nicer namespace ("My.Computer") that comes along
with Visual Studio 2005:
' Delete file only it if it exists already (otherwise you'd
get an exception).
If My.Computer.FileSystem.FileExists(strFullFileName) Then
My.Computer.FileSystem.DeleteFile(strFullFileName)

Regards,
ImageAnalyst
 
---------------------------------------------------------------------------­-----------------
za how can I call this function in the event handler?

I thank you so much

When I am using the File object in an app, first of all, I include an:

Imports System.IO

Then, in an event handler or any other sub or function, as long as the
code has the full path to the file, just invoke the .Delete method of
the File.Class. You do not need to instantiate an object of the File
Class. But as another poster recommended, to avoid an unhandled
exception either test to see if the file exists first or not, or wrap
the .Delete method call in a Try/Catch.

if File.Exists("path to file") then File.Delete("path to file")

or

Try
File.Delete("path to file")
Catch ex as Exception
' do what ever you need to do, display an error message box or simply
put no code here at all to ignore it
End Try
 
Back
Top