Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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

Posted

You can't 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, you'll see the File class that has a Delete(string path) static function.

"Who is John Galt?"
  • 5 months later...
Posted

@ PlausiblyDamp

Hi, I'm wondering if you can help me with a problem I have?

 

I'm trying to open an executable file which copies some files & deletes the whole directory including the file that is originally opened. I'm 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 I'm finished with them?

 

Your assistance is appreciated.

 

Thank you

Posted
I believe you'll have to launch a seperate thread, then close the initial file which will give you access to delete it.
Anybody looking for a graduate programmer (Midlands, England)?
Posted
My answer was assuming by 'initial file' you meant the executable, which if I'm not mistaken could not be deleted with the method DiverDan describes.
Anybody looking for a graduate programmer (Midlands, England)?
Posted
True enough, I was replying to SimDucks questions however and was merely clarifying the point incase they tried your suggestion.
Anybody looking for a graduate programmer (Midlands, England)?
Posted

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 exe's job is to copy the files to a specified directory & then delete the whole folder including the exe.

 

Unfortunately, I can't 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 I'm not very experiend with vb.net programming. I'm trying to self learn & I'm 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?

 

I'm open to suggestions to solve my problem.

 

Thank you

  • *Experts*
Posted
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.

Member, in good standing, of the elite fraternity of mentally challenged programmers.

 

Dolphins Software

Posted

DriverDan, this sounds like an excellent idea. I have tried to do this but I don't know how to call a dll with out it being referenced to the calling form. :confused:

 

This is my exit sub routine:

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

  • *Experts*
Posted (edited)

Well you're right SimDuck, can't do it with a dll. But I did create an exe that would.

 

From you're current form's button click sub:

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:

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 don't know if this will fix your application, but it works.

And exchange "WindowsApplication1" with your project's exe name. Also, this example only deletes the calling program. You can obviously change the deleation process to a directory.

Edited by DiverDan

Member, in good standing, of the elite fraternity of mentally challenged programmers.

 

Dolphins Software

Posted

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". I'm thinking that I'll move the file first & then use my application to delete it upon its loading.

 

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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...