Saturday, November 19, 2005
Cannot delete fileX :Access is Denied
How many times did you get this message ?
"Cannot delete fileX :Access is Denied
Make Sure the Disk is not full or write-protected
And the file is not currently in use"
I think a lot ha ?
At last I got a solution from the BCL Team
you can use it to enumerate the processes that are using the blocked module
For example : LMOD.exe file1.dll
Then you get a list of running process who are using file1.dll module
/// <summary>
/// Simple tool to find out which process have loaded a particular module.
/// </summary>
public class LMod
{
// "System Idle Process" pid
static int IdleProcessID = 0;
// "System" pid
static int SystemProcessID
{
get
{
//Is older than XP...
if (Environment.OSVersion.Version.Major < 5 ||
(Environment.OSVersion.Version.Major == 5 &&
Environment.OSVersion.Version.Minor == 0))
return 8;
else
return 4;
}
}
public static int Main(string[] args)
{
int total = 0;
string m_ModuleName = "";
if (args.Length == 1)
m_ModuleName = args[0];
else
{
// wrong number of parameters...
Console.WriteLine("Usage: LMod module_name");
return 1;
}
// Get all running processes on the machine...
Process[] m_arrSysProcesses = Process.GetProcesses();
for (int i = 0; i < m_arrSysProcesses.Length; i++)
{
try
{
ProcessModuleCollection modules =
m_arrSysProcesses[i].Modules;
int nCount = modules.Count;
if (nCount > 0)
{
for (int j = 0; j < nCount; j++)
{
// Is it the module we are looking for?
if (modules[j].ModuleName == m_ModuleName)
{
Console.WriteLine("-------------------");
Console.WriteLine("Process Name: "
+ m_arrSysProcesses[i].ProcessName);
Console.WriteLine("Process ID : "
+ m_arrSysProcesses[i].Id);
Console.WriteLine("Priority : "
+ m_arrSysProcesses[i].BasePriority);
Console.WriteLine("Memory Usage: "
+ (m_arrSysProcesses[i].WorkingSet64 /
1024) + " Kb");
Console.WriteLine();
total++;
break;
}
}
}
}
catch (Exception e)
{
// System Idle Process (Idle): represents pseudo-process
// that represents all the processor time not used by
// other processes.
// System (System): represents the processor time
// used by the kernel itself.
if (m_arrSysProcesses[i].Id != SystemProcessID
&& m_arrSysProcesses[i].Id != IdleProcessID)
{
Console.WriteLine("Error: Process "
+ m_arrSysProcesses[i].ProcessName
+ " (" + m_arrSysProcesses[i].Id + ") failed!");
Console.WriteLine(e);
return 2;
}
}
}
Console.WriteLine();
Console.WriteLine("There are " + total
+ " processes using module " + m_ModuleName);
return 100;
}
}