Click here to Skip to main content
15,887,350 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I wanted to delete all temporary files in the temp folder of the current user. For this purpose I wrote the following code
C#
string s = System.IO.Path.GetTempPath();
Console.WriteLine(s);
DirectoryInfo di = new DirectoryInfo(s);
FileInfo[] fi = di.GetFiles();
foreach (FileInfo f in fi)
{
  f.Delete();
}

I then was faced with a problem that some files may be used by some processes and finally they won't be deleted and an exception would be trigger at the line
C#
f.delete()

I thought that I'd better get the process ID that catches each file and kill it. The question is how to get that process id.

OP's additional information moved from non-solution below
When I know the process that catches a file I can activate the screen that represent this process and ask the user to save/cancel , or if the process in the background I can warn the user that the this process catches this file and you may lost work. This program with this ability would be similar to the SHUT DOWN warning process that the windows do.
Posted
Updated 15-Dec-12 22:08pm
v2

Why would you want to delete temporary files which are in use? Since you do not know what the process is doing with them, you could potentially lose the user a lot of work by identifying and killing the process.

Why not just detect the locked file, and ignore it? Use a try...catch block round the delete and ignore the exception (but be sure to comment why you ignore it!)
 
Share this answer
 
Comments
Nelek 15-Dec-12 13:13pm    
Good advice.
Jibesh 15-Dec-12 16:31pm    
Agree with you Griff.
[no name] 16-Dec-12 0:28am    
my vote Griff
You can use Process.Id Property[^] or else Process.GetProcesses[^]

Hope it will help you... :)
 
Share this answer
 
I think this is what you need. http://www.kartmann.org/freeware/WhoSLocking/ReadMe.htm[^]

The process who locked your file trying to delete. they have share the source code too. have a look at that.
 
Share this answer
 
I think intially you need to know whether the file is in use and if not you can .
delete it.

Add this function which returns whether the file is in open.


C#
public static bool IsFileInUse(string pathToFile)
    {
        if (!System.IO.File.Exists(pathToFile))
        {
            // File doesn't exist, so we know it's not in use.
            return false;
        }

        bool inUse = false;
        System.IO.FileStream fs;
        try
        {
            fs = System.IO.File.Open(pathToFile, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Read, System.IO.FileShare.None);
            fs.Close();

DirectoryInfo di = new DirectoryInfo(pathToFile));
FileInfo[] fi = di.GetFiles();
foreach (FileInfo f in fi)
{
  f.Delete();
}
        }
        catch (System.IO.IOException ex)
        {
            string exMess = ex.Message;
            inUse = true;
        }
        return inUse;
    }




If the file is in open and if you know the process name which it takes in task manager you can forcefully delete the process which releases the files.

Code for killing that particular process.

Here I've taken Acrobat reader as the process which opens the PDF files.


C#
public void KillProcess()
{
    try
    {
        foreach (Process process in Process.GetProcesses())
        {
            if (process.ProcessName == "AcroRd32")
            {
                process.Kill();
            }
        }
    }
    catch (Exception exception)
    {
        
    }
}
 
Share this answer
 
v5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900