Results 1 to 14 of 14

Thread: Problem Deleting Folder. PLEASE HELP

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2005
    Posts
    181

    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?

  2. #2
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    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:
    1. 'after deleting the file, sleep it for 100 milliseconds...
    2. System.Threading.Thread.Sleep(100)
    3. '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??

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    May 2005
    Posts
    181

    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

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Problem Deleting Folder. PLEASE HELP

    Quote 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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6
    Frenzied Member conipto's Avatar
    Join Date
    Jun 2005
    Location
    Chicago
    Posts
    1,175

    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:
    1. 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
    Hate Adobe Acrobat? My Codebank Sumbissions - Easy CodeDom Expression evaluator: (VB / C# ) -- C# Scrolling Text Display

    I Like to code when drunk. Don't say you weren't warned.

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    May 2005
    Posts
    181

    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?

  8. #8
    Frenzied Member conipto's Avatar
    Join Date
    Jun 2005
    Location
    Chicago
    Posts
    1,175

    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
    Hate Adobe Acrobat? My Codebank Sumbissions - Easy CodeDom Expression evaluator: (VB / C# ) -- C# Scrolling Text Display

    I Like to code when drunk. Don't say you weren't warned.

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    May 2005
    Posts
    181

    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.

  10. #10
    Frenzied Member vbdotnetboy's Avatar
    Join Date
    Jun 2004
    Location
    Lewisburg, PA "Next year Raiders in the Super Bowl"
    Posts
    1,310

    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.

    Derek - Using VS 2008 99% of the time and VS 2003 1% of the time

    Please Help Us To Save Ana

    ● Helpful Links: DNR TV | Awesome site for tips | Using ADO.NET to work with Excel | Xml Namespace 2.0 Framework Changes|Ultra High Security Password Generator | Mendhak's ADO.NET Tutorial
    ● Code Bank: Random Password Generator | Generic DbProviderFactory Access
    ● Site Work: Bottle Run Xtreme | Spaids Racing.com

    Company I work for - CSSI

    WHEN POSTING PLEASE INDICATE VERSION

    Please use vbcode tags or code tags when posting code
    [highlight=vb]ALL your code goes here[/highlight] or [code]ALL your code goes here[/code]

    If my post helped you in anyway... please be kind and give me some ratings

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    May 2005
    Posts
    181

    Re: Problem Deleting Folder. PLEASE HELP

    any ideassssssssssss

  12. #12

    Thread Starter
    Addicted Member
    Join Date
    May 2005
    Posts
    181

    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...

  13. #13
    Frenzied Member conipto's Avatar
    Join Date
    Jun 2005
    Location
    Chicago
    Posts
    1,175

    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
    Hate Adobe Acrobat? My Codebank Sumbissions - Easy CodeDom Expression evaluator: (VB / C# ) -- C# Scrolling Text Display

    I Like to code when drunk. Don't say you weren't warned.

  14. #14

    Thread Starter
    Addicted Member
    Join Date
    May 2005
    Posts
    181

    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
  •  



Click Here to Expand Forum to Full Width