dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: Force Delete File

  1. #1
    Join Date
    Oct 2007
    Posts
    60

    Force Delete File

    Hi,

    I am trying to delete a file but my code always throws an exception. I know the file is being used by another program but because of a bug in that other program (which I didn't write) I am unable to delete the file unless I first exit the program. Is there a way I could first unlock the file and then delete it in C#? (Deleting this is completely safe IMHO because nothing is being written into the file)

  2. #2
    Join Date
    Oct 2008
    Posts
    8

    Re: Force Delete File

    What if you keep it alive on computer ? If you insist on doing it, then reboot your computer to see if this second try works out for you

  3. #3
    Join Date
    Mar 2008
    Location
    IRAN
    Posts
    811

    Re: Force Delete File

    assume the process that use the file you want to delete is notepad
    then first you should kill the process and then try to delete the file.

    use this code to kill the related process:

    Code:
                Process[] prcList  = Process.GetProcesses();
    
                foreach (Process p in prcList)
                {
                    if (p.ProcessName == "notepad")
                    {
                        p.Kill();
                    }
                }
    don't forget to add this using statement at top of your source file :
    using System.Diagnostics;
    Please rate my post if it was helpful for you.
    Java, C#, C++, PHP, ASP.NET
    SQL Server, MySQL
    DirectX
    MATH
    Touraj Ebrahimi
    [toraj_e] [at] [yahoo] [dot] [com]

  4. #4
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,268

    Re: Force Delete File

    Nice solution toraj!

    You could also use the MoveFileEx API with the MOVEFILE_DELAY_UNTIL_REBOOT Flag
    [SIGPIC][/SIGPIC]
    All my Articles
    Hannes

  5. #5
    Join Date
    Oct 2007
    Posts
    60

    Re: Force Delete File

    Thanks a lot for the solution. I have a small question at this stage. So what is happening is that the program is writing some data into a file. What I intend to do is to periodically clear the file and make a backup.

    Now from your solution, I understood that we need to kill the process before I can do anything with the file. In this case, could you suggest an alternative to achieve this? If it is not possible, I will try to solve the problem from a different angle. Just that I need to know that this is not possible in the first place.

    PS: Thank you again for the solution. Now I know partly how those unlocker programs that delete in use files work

  6. #6
    Join Date
    Mar 2008
    Location
    IRAN
    Posts
    811

    Re: Force Delete File

    Please rate my post if it was helpful for you.
    Java, C#, C++, PHP, ASP.NET
    SQL Server, MySQL
    DirectX
    MATH
    Touraj Ebrahimi
    [toraj_e] [at] [yahoo] [dot] [com]

  7. #7

    Re: Force Delete File

    an alternative to achieve this?
    With some work, you should be able to use P/Invoke to do this... Call NtQuerySystemInformation to enumerate all of the handles in the process, looking for the file of interest. Then, call DuplicateHandle with the DUPLICATE_CLOSE_SOURCE flag. Close the target handle, then delete the file. Note that this may have the effect of introducing instabilities into the application that previously had a handle to the file, such that it may crash or exhibit other anomalous behavior (someone closed the handle out from underneath it, and it may still try to use the handle)...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)