How to delete a file in VB.NET

laroberts

Active member
Joined
Jun 29, 2005
Messages
34
I am having a problem trying to delete a file from within VB.NET. Below is the code I have now....


Can somebody tell me why the deletefile is not accepted. I know you can do a savefile or loadfile... How can i set it to delete a file?

Thank you


Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click

Delete Monday Third Shift Job Number

Dim textbox3 As String
If textbox3 = "4535" Then
Dim selecteditem As String = CStr(Me.ComboBox3.SelectedItem)
Dim selectedjobnum As Integer = selecteditem.Substring(selecteditem.IndexOf("#"c) + 1)
Process.Start(String.Format("E:\Job{0}.rtf", selectedjobnum))
RichTextBox2.DeleteFile(String.Format("J:\OperationsGenie\JobsHelp\Monday\ThirdShift\{0}.rtf", selectedjobnum))
Else
MsgBox("You have entered an invalid Code. Please enter the code located is the validation box to proceed.")
End If
End Sub
 
You cant just make up function names and hope the class supports it. Intellisense and Object Browser are your friends.

Rightly so, RichTextBox has nothing to do with deleting files. Look in the System.IO namespace, youll see the File class that has a Delete(string path) static function.
 
Whats not to understand? Look in the System.IO namespace for the File class, it has a method called Delete that lets you delete a file.
 
@ PlausiblyDamp
Hi, Im wondering if you can help me with a problem I have?

Im trying to open an executable file which copies some files & deletes the whole directory including the file that is originally opened. Im getting an error saying that the file is in use.

My question is, is there a way that I can run a file, script or code & put it in to memory & run it from memory so I can delete the files once Im finished with them?

Your assistance is appreciated.

Thank you
 
Youll need to load the file with a stream reader into a variable. Then close the stream reader to release the file for deletion.
 
My answer was assuming by initial file you meant the executable, which if Im not mistaken could not be deleted with the method DiverDan describes.
 
I could be wrong but ("J:\OperationsGenie\JobsHelp\Monday\ThirdShift\{0}.rtf" looks like an .rtf file to me and not an executable.
 
Thank you Cags & DiverDan.

Cags, you are correct. I have created an *.exe with vb.net. I am extracting the exe to a temp folder (along with some other files) & then running the exe. The exes job is to copy the files to a specified directory & then delete the whole folder including the exe.

Unfortunately, I cant work out how to delete an open file or load the exe purely into memory so I can delete it later. I want the copying/ patching procedure to be clean

I have tried the threading but Im not very experiend with vb.net programming. Im trying to self learn & Im trying different ways to achieve what I want.

I have had an idea that there might be a way that I can set a windows setting that will delete the file upon next load. Is this possible & if so, how do I do it?

Im open to suggestions to solve my problem.

Thank you
 
One very simple thought is to have your .exe app call a .dll app on closing. The .dll app would start with a timer then delete the required directory.
 
DriverDan, this sounds like an excellent idea. I have tried to do this but I dont know how to call a dll with out it being referenced to the calling form. :confused:

This is my exit sub routine:
Code:
Private sub btnFinish_Click(ByVal sender as System.objecti, ByVal e as System.EventArgs) Handles btnFinish.Click

startform()
Me.Dispose()
End Sub

Private Sub startform()
Dim frmPatchClean as new Patcherdll.frmPatchDllMain(me)
frmPatchClean.show
End Sub

I have the delete folder code in the dll with the timer

How do I call a dll without having a reference to it ?

Thank you
 
Well youre right SimDuck, cant do it with a dll. But I did create an exe that would.

From youre current forms button click sub:
Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    If System.IO.File.Exists(Application.StartupPath & "\DeleteExe.exe") Then
        System.Diagnostics.Process.Start(Application.StartupPath & "\DeleteExe.exe")
    End If
End Sub

Then with a new project named DeleteExe, set the form to bordless with a size of 0,0
In its Form Load sub:
Code:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    Dim proc As System.Diagnostics.Process() = System.Diagnostics.Process.GetProcessesByName("WindowsApplication1")

    If proc.GetUpperBound(0) >= 0 Then
        proc(0).Close()

        If System.IO.File.Exists(Application.StartupPath & "\WindowsApplication1.exe") Then
            System.IO.File.Delete(Application.StartupPath & "\WindowsApplication1.exe")
        End If
    End If
    Me.Close()
End Sub

I dont know if this will fix your application, but it works.
And exchange "WindowsApplication1" with your projects exe name. Also, this example only deletes the calling program. You can obviously change the deleation process to a directory.
 
Last edited by a moderator:
Thank you. This works well. I have copied the code & created the new exe. It deletes the file beautifully. I modified the DeleteExe.exe code to delete the whole directory (DeleteExe.exe is in this directory).

When it gets to the delete command, it deletes every file except for the DeleteExe.exe file throwing the exception "Access is denied". Im thinking that Ill move the file first & then use my application to delete it upon its loading.

Thank you heaps for your assistance. I appreciat it :)
 
Back
Top