Note that there are some explanatory texts on larger screens.

plurals
  1. POIssues about files or folders in use: get the name of another Process that use file or folder
    primarykey
    data
    text
    <p>I using C# .NET , vs 2008 , .net 3.5</p> <p>For me, is difficult, but I need sample code in C# for this:</p> <ol> <li><p>Check if a file or a folder is in use</p></li> <li><p>If file or a folder is in use, the name of Process that use it</p></li> </ol> <p>For example, in my issue. </p> <p>I try delete file, and I get "The process cannot access the file 'XYZ' because it is being used by another process." Exception.</p> <p><code>File.Delete(infoFichero.Ruta);</code></p> <p>I want check if a file is in use, and the name of Process that use it.</p> <p>I need sample code, source code, please. I dont want use c++, I dont know c, c++, unmanaged code, or WinApi. I want use only C# code (managed code .net).</p> <p>I have read several references but not get sample code source,</p> <p>How to check if a file is in use?</p> <p><a href="https://stackoverflow.com/questions/3138483/emulate-waiting-on-file-open-in-c-when-file-is-locked">Emulate waiting on File.Open in C# when file is locked</a></p> <p><a href="http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/9dabc172-237a-42db-850e-ada08885a5d5" rel="nofollow noreferrer">http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/9dabc172-237a-42db-850e-ada08885a5d5</a></p> <p><a href="https://stackoverflow.com/questions/1314958/how-to-check-if-a-file-is-in-use">How to check if a file is in use?</a></p> <p><a href="https://stackoverflow.com/questions/1389155/easiest-way-to-read-text-file-which-is-locked-by-another-application">Easiest way to read text file which is locked by another application</a></p> <p><a href="https://stackoverflow.com/questions/698442/using-c-is-it-possible-to-test-if-a-lock-is-held-on-a-file">Using C# is it possible to test if a lock is held on a file</a></p> <p>EDIT: From Yan Jun - MSFT</p> <pre><code> string path = "D:\\temp2.xlsx"; foreach (Process c in Process.GetProcesses()) { if (c.MainWindowTitle.Contains(Path.GetFileName(path))){ MessageBox.Show(c.ProcessName); return; } } try{ FileInfo f = new FileInfo(path); f.Delete(); } catch (Exception ex){ MessageBox.Show(ex.Message); } </code></pre> <p>... But it is difficult get solution for all 100% issues.</p> <ol> <li><p>Problem if c.MainWindowTitle == null or not contains filename.</p></li> <li><p>Problem for shared folder in another machine, PC, server,... like: </p></li> </ol> <p>File.Delete(@\desiis\TEmporal\Project\script.targets);</p> <p>any sample code, I ask for help gurus, MVPs, anyone.</p> <p><strong>UPDATE: the same issue for a folder</strong></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload