laroberts Posted July 4, 2005 Posted July 4, 2005 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 Quote
IngisKahn Posted July 4, 2005 Posted July 4, 2005 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. Quote "Who is John Galt?"
Administrators PlausiblyDamp Posted July 4, 2005 Administrators Posted July 4, 2005 What's 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. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
SimDuck Posted December 8, 2005 Posted December 8, 2005 @ 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 Quote
Cags Posted December 8, 2005 Posted December 8, 2005 I believe you'll have to launch a seperate thread, then close the initial file which will give you access to delete it. Quote Anybody looking for a graduate programmer (Midlands, England)?
*Experts* DiverDan Posted December 8, 2005 *Experts* Posted December 8, 2005 You'll need to load the file with a stream reader into a variable. Then close the stream reader to release the file for deletion. Quote Member, in good standing, of the elite fraternity of mentally challenged programmers. Dolphins Software
Cags Posted December 9, 2005 Posted December 9, 2005 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. Quote Anybody looking for a graduate programmer (Midlands, England)?
*Experts* DiverDan Posted December 9, 2005 *Experts* Posted December 9, 2005 I could be wrong but ("J:\OperationsGenie\JobsHelp\Monday\ThirdShift\{0}.rtf" looks like an .rtf file to me and not an executable. Quote Member, in good standing, of the elite fraternity of mentally challenged programmers. Dolphins Software
Cags Posted December 9, 2005 Posted December 9, 2005 True enough, I was replying to SimDucks questions however and was merely clarifying the point incase they tried your suggestion. Quote Anybody looking for a graduate programmer (Midlands, England)?
SimDuck Posted December 9, 2005 Posted December 9, 2005 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 Quote
*Experts* DiverDan Posted December 10, 2005 *Experts* Posted December 10, 2005 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. Quote Member, in good standing, of the elite fraternity of mentally challenged programmers. Dolphins Software
SimDuck Posted December 10, 2005 Posted December 10, 2005 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 Quote
*Experts* DiverDan Posted December 10, 2005 *Experts* Posted December 10, 2005 (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 December 10, 2005 by DiverDan Quote Member, in good standing, of the elite fraternity of mentally challenged programmers. Dolphins Software
SimDuck Posted December 10, 2005 Posted December 10, 2005 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 :) Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.