Ivan
Ivan

Reputation: 1171

How to delete files programmatically?

Here is the code that I use to close my application, its associated processes and to delete all the files that have been extracted during the use of the application:

private void Quit_Click(object sender, RoutedEventArgs e) //close the application
{
    //kill cinector after all import is done
    Process[] processes = Process.GetProcesses();
    for (int i = 0; i < processes.Count(); i++)
    {
        if (processes[i].ProcessName.ToLower().Contains("CinectorProcess"))
        {
            processes[i].Kill();
        }
    }

    //also kill powerpoint just in case

    for (int i = 0; i < processes.Count(); i++)
    {
        if (processes[i].ProcessName.ToLower().Contains("powerpnt"))
        {
            processes[i].Kill();
        }
    }

    //kill the engine
    ShutdownEngine();

    //kill the main app
    App.Current.Shutdown();

    //also delete all three folders

    //slides_png_prev
    if (Directory.Exists(slides_png_prev))
    {
        Thumbnails = null;
        Directory.Delete(slides_png_prev, true);
    }

    //slides_png
    if (Directory.Exists(slides_png))
    {
        Directory.Delete(slides_png, true);
    }

    //slides_png_prev_seleect
    if (Directory.Exists(slides_png_prev_seleect))
    {
        Directory.Delete(slides_png_prev_seleect, true);
    }
}

However, the problem is that when it tries to delete the files (which are the images used somewhere in the app) it shows the following exception:

"The process cannot access the file because it is being used by another process."

Update: I found that the process 'Mastersolution.vhost.exe' is holding all the files that I am attempting to delete. Mastersolution is actually the main app that I am closing on the line App.Current.Shutdown(); So I need to somehow disconnect the files from the main application prior to deleting them. But hoe to do this?

Upvotes: 5

Views: 1114

Answers (3)

RubberDuck
RubberDuck

Reputation: 12788

Store your files in a temp directory and let the operating system handle it.

See Path.GetTempPath and Path.GetTempFileName.

Upvotes: 3

James
James

Reputation: 9985

You can delete folders that way without issue provided they're not in use and you have permission to do so. You can use SysInternals Handle to determine which processes are accessing the file/folder you want. Note that if you can't delete the files and it's not your process accessing them, just raise a warning: "Could not clean up folder: Slide Previews" Instead of randomly killing the users apps until you can delete a png.

On a side note: Remind me not to use an application that can kill office and ditch any work I've done.

You should use the pid's of each process you start or better yet, use IPC to send a friendly shutdown request which can clean up resources before closing. For powerpoint this is a simple as calling Quit() on the application object.

Upvotes: 0

Vojtěch Dohnal
Vojtěch Dohnal

Reputation: 8104

First you should determine, which file in the folder is locked - instead of deleting whole folder, delete individual files. Then check with Process Explorer and Ctrl+F what process really uses the undeletable file.

Also you can use the method WhoIsLocking in this code.

https://bitbucket.org/snippets/wiip/nnGX

You can use it to determine, which process locks the file. Then you can write something like this to kill this process automatically:

         List<Process> lp = FileUtil.WhoIsLocking(fileName);
         foreach (Process p in lp)
         {
                 p.Kill();
         }

This is last workaround when there are no better options, like closing the file in your app, closing Powerpoint using automation etc.

Upvotes: 3

Related Questions