I am writing within a heavily managed async (single) thread. it is a FIFO queue in a thread and something
is locking my attempt to do a Directory.SetLastWriteTime
. It is almost certainly me. but I can't see it. I used this link: and as part of it I tried to use Handle
but c# wouldn't understand the reference to handle.exe
(failed at line tool.Start();
) despite the path to handle.exe being in the system path and it running fine in command.
The problem is I then tried to use the FileUtil
example code lower down the answers list but this only works on files
I need to see what is locking a folder
. bear in mind this folder is not really locked, I can change the LastModifiedDate
in Attribute Changer 7
so it is certainly my queue that is dong it wrong.
this is the code I am actually trying to run, that continually says it is locked.
System.DateTime newDateTime = new DateTime(2011,1,1,0,0,0,0,System.DateTimeKind.Utc);
if (Directory.Exists(parent))
{
Unlocker.unlock(parent); //<--new line
Directory.SetLastWriteTime(parent, newDateTime);
}
note that I suspect FileUtil
works fine for files but I need to modify a folder
and it is bombing badly. Can anyone help me get the Process locking a given directory not just a file?
EDIT (see also, <<- new line
above) I have abandoned FileUtil
but I am now using handle.exe
successfully (i.e. if I manually lock it by trying to to give it mouse focus in windows explorer for example it detects it) but handle returns no "locks" when the c# bombs:
public static class Unlocker
{
public static void unlock(string fileName)
{
Process tool = new Process();
tool.StartInfo.FileName = @"C:\Program Files (x86)\Handle\handle.exe";
tool.StartInfo.Arguments = fileName + " /accepteula";
tool.StartInfo.UseShellExecute = false;
tool.StartInfo.RedirectStandardOutput = true;
tool.Start();
tool.WaitForExit();
string outputTool = tool.StandardOutput.ReadToEnd();
string matchPattern = @"(?<=\s+pid:\s+)\b(\d+)\b(?=\s+)";
foreach (Match match in Regex.Matches(outputTool, matchPattern))
{
Debug.WriteLine("Unlocker.unlock(`" + fileName + "`) is locked by `" + Process.GetProcessById(int.Parse(match.Value)).ProcessName + "`");
}
}
}
SECOND EDIT the SetWriteTime was working all along, it was a looping issue further up the code line, it didn't work when the user has mouse clicked on the folder (which I had been doing accidentally randomly) but the bad outer loop was perpetuating it - a bad finally forever causing a loop counter exception with a faulty "file is locked" message.