Check who has taken the handle of the text file.
-
Hi all, I have one text file. now i want to check who has taken the handle of the text file. How can i find the exe in C#? Reply ASAP. Thanking You, Sunil G.
modified on Tuesday, February 23, 2010 2:10 AM
-
To retrieve a list of running processes use:
System.Diagnostics.Process.GetProcesses()
which returns an array avProcess
objects.with this System.Diagnostics.Process.GetProcesses() i get all the running processes. but i want the process name who has taken the handle of particular text file. As in input parameter i will give textfile name and want the process who has taken the handle of the textfile.
modified on Tuesday, February 23, 2010 3:35 AM
-
with this System.Diagnostics.Process.GetProcesses() i get all the running processes. but i want the process name who has taken the handle of particular text file. As in input parameter i will give textfile name and want the process who has taken the handle of the textfile.
modified on Tuesday, February 23, 2010 3:35 AM
-
Do you have the name of the text file and you want to retrieve the process which has the handle to that file? Could you please elaborate more?
Yes you r on the track. I have the name of the text file and I want to retrieve the process which has the handle to that file.. If you want more explanation, i will explain the whole case. Thanking You, Sunil G.
modified on Tuesday, February 23, 2010 3:46 AM
-
Yes you r on the track. I have the name of the text file and I want to retrieve the process which has the handle to that file.. If you want more explanation, i will explain the whole case. Thanking You, Sunil G.
modified on Tuesday, February 23, 2010 3:46 AM
-
Hi all, I have one text file. now i want to check who has taken the handle of the text file. How can i find the exe in C#? Reply ASAP. Thanking You, Sunil G.
modified on Tuesday, February 23, 2010 2:10 AM
This is inposible to do it completly in C#. You must not be afrait to use a lot of PInvoke I hope this a few articles help you: (I think the last link will be best) http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/9e2044c5-ae5d-4552-a335-01cc567dfc58[^] http://stackoverflow.com/questions/242882/how-can-i-unlock-a-file-that-is-locked-by-a-process-in-net[^] http://www.codeguru.com/Cpp/W-P/files/fileio/article.php/c1287/#more[^] http://www.vbforums.com/showthread.php?t=501992[^]
-
This is inposible to do it completly in C#. You must not be afrait to use a lot of PInvoke I hope this a few articles help you: (I think the last link will be best) http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/9e2044c5-ae5d-4552-a335-01cc567dfc58[^] http://stackoverflow.com/questions/242882/how-can-i-unlock-a-file-that-is-locked-by-a-process-in-net[^] http://www.codeguru.com/Cpp/W-P/files/fileio/article.php/c1287/#more[^] http://www.vbforums.com/showthread.php?t=501992[^]
-
I'm sorry Sunil - don't know how to achieve that. Hope someone else can help, and I'l make a post if I have a sudden stroke of genious.. Good luck anyway!
Hi Check this out it might help you..i hav'nt tried it but looks like this is what you are looking for.. fpound this solution while going thrw some blog :) posted by :-Gennady Zabrodsky It is very complex way to invoke Win32 from C#. You should use tool Handle.exe from http://technet.microsoft.com/en-us/sysinternals/bb896655.aspx After that your C# code have to be the following: string fileName = @"c:\aaa.doc";//Path to locked file Process tool = new Process(); tool.StartInfo.FileName = "handle.exe"; tool.StartInfo.Arguments = fileName; 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)) { Process.GetProcessById(int.Parse(match.Value)).Kill(); }