|
-
Jan 14th, 2006, 11:25 PM
#1
Thread Starter
Addicted Member
Problem Deleting Folder. PLEASE HELP
Hey. I'm having the most trouble ever trying to delete a folder. Here's what I have for code:
If (IO.File.Exists("C:\Documents and Settings\" & Environment.UserName & "\Local Settings\Temp\program\Updater.exe")) = True Then
IO.File.Delete("C:\Documents and Settings\" & Environment.UserName & "\Local Settings\Temp\program\Updater.exe")
End If
If (IO.Directory.Exists("C:\Documents and Settings\" & Environment.UserName & "\Local Settings\Temp\Updater\") = True) Then
IO.Directory.Delete("C:\Documents and Settings\" & Environment.UserName & "\Local Settings\Temp\updater\")
End If
The first part deletes the file within the folder. Then the second part deletes the folder itself. It deletes teh file ok, but whenever it gets to the folder it says its in use! If i click continue and continue with using the program, i can IMMEDIATELY delete the folder-- as if its not in use! Any ideas?
-
Jan 14th, 2006, 11:29 PM
#2
Re: Problem Deleting Folder. PLEASE HELP
maybe its some sort of timing issue?? you can try to sleep the thread for a few milliseconds, see if that helps at all...
VB Code:
'after deleting the file, sleep it for 100 milliseconds...
System.Threading.Thread.Sleep(100)
'now call your folder delete code...
You can play with the milliseconds, increase it or decrease it... see if it helps at all. Not sure if it will, but its worth a shot...
Also, are you sure the Updater.exe program has finished executing when you were calling your above code??
-
Jan 15th, 2006, 02:05 AM
#3
Re: Problem Deleting Folder. PLEASE HELP
Why even bother with deleting the file? Directory.Delete is overloaded and allows you to delete files and subdirectories too.
-
Jan 15th, 2006, 05:07 PM
#4
Thread Starter
Addicted Member
Re: Problem Deleting Folder. PLEASE HELP
hey. I tried that but it doesnt work. It gives me an error and says that the directory isn't empty
-
Jan 15th, 2006, 05:17 PM
#5
Re: Problem Deleting Folder. PLEASE HELP
 Originally Posted by JohnRChick
hey. I tried that but it doesnt work. It gives me an error and says that the directory isn't empty
Are you using the overload that takes two parameters? The one that has this description in the help:
Deletes the specified directory and, if indicated, any subdirectories in the directory.
[Visual Basic]
Overloads Public Shared Sub Delete( _
ByVal path As String, _
ByVal recursive As Boolean _
)
Parameters
path
The name of the directory to remove.
recursive
true to remove directories, subdirectories, and files in path; otherwise, false.
-
Jan 15th, 2006, 05:19 PM
#6
Re: Problem Deleting Folder. PLEASE HELP
As jmcilhinney said, there are two different overloads for IO.Directory.Delete
http://msdn2.microsoft.com/en-us/lib...ry.delete.aspx
If you use the second one, and specify true:
VB Code:
IO.Directory.Delete("C:\MyTempDir" True)
It will delete it whether or not it is empty. The Overload list on MSDN doesn't specify that it deletes files, only directories, so it is perhaps a bit misworded on MSDN. However, I just tested it with a directory containing a file, and it works fine.
Bill
-
Jan 16th, 2006, 12:03 AM
#7
Thread Starter
Addicted Member
Re: Problem Deleting Folder. PLEASE HELP
ok I'm still having trouble! Heres my situation. I have an updater program that downloads a new verison and replaces the old one with the new one. Then it runs the new one and closes itself. I set the new program to delete the temporary files and thast where I'm having trouble. It can delete the temporary update program fine, but just not the folder it's in! Here's the real strange part. I tried putting the delete code in a timer and have it try and delete it every second. It can never delete the folder. If I try and delete it manually through explorer I can delete it though! I don't get it!!! Any ideas?
-
Jan 16th, 2006, 01:18 AM
#8
Re: Problem Deleting Folder. PLEASE HELP
Post the code you launch the second file with.. is it possible the other EXE is still in use waiting for program two to exit, and therefor, not deletable?
Bill
-
Jan 16th, 2006, 11:37 AM
#9
Thread Starter
Addicted Member
Re: Problem Deleting Folder. PLEASE HELP
ok so heres what i have:
main program launches updater: Shell(tempDIR & "Updater.exe -startUpdate")
note: -startUpdate is used so that the program can't be run unless called by main program
updater calls original program:
Shell(currentDir & "\" & "myprogram.exe -cleanup")
Close()
Note: -cleanup is used to tell the main program to delete the folder.. and thats where I have the problem.
As you can see though, the program is closed. Even though it closes after the other one launches, i tried using a time to wait for it to be completely closed, but it still doesn't work.
-
Jan 16th, 2006, 11:40 AM
#10
Re: Problem Deleting Folder. PLEASE HELP
so the program that suppose to do the deleting is the one that your having problems with? if so i think that's what conipto was asking for.
-
Jan 16th, 2006, 02:02 PM
#11
Thread Starter
Addicted Member
Re: Problem Deleting Folder. PLEASE HELP
-
Jan 16th, 2006, 05:43 PM
#12
Thread Starter
Addicted Member
Re: Problem Deleting Folder. PLEASE HELP
could it be the problem that in teh updater program most of hte code is run in a separate thread? The thing is that i'm ending the thread also as I close the application. Just a thought though...
-
Jan 16th, 2006, 05:49 PM
#13
Re: Problem Deleting Folder. PLEASE HELP
The way I see it, you have something like this:
Code:
FlowChart:
Main Program calls updater
|
updater calls main program with instruction to end.
|
New instance of main program - with command to delete update
|
Delete Updater.
|
updater program closes
|
|Main program closes
Which can't work. (because you delete before program 2 is closed.
I think your problem lies in using the shell command. I believe your two programs are waiting for the shell command to end before they will leave memory. Isn't there a way to launch an app in another thread, and terminate the current thread? I don't have much experience writing multi-threaded apps.
Bill
-
Jan 16th, 2006, 06:39 PM
#14
Thread Starter
Addicted Member
Re: Problem Deleting Folder. PLEASE HELP
hmm this is weird.... because if i just try and delete the updater program I can do that, but not the actual folder its in! Once I have the main program running after the update, I have it designed to try to delete the program and folder, and if it can't it gives me the error message. The thing is, right after the error message if I try and delete them myself i can actually do it. It's so strange. Any ideas on another way to delete them? I might be forced just to put smething in the registry and have it check that.. thats a last resort
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|