Get the name of Process use files and issues

Closed
kiquenet Posts 3 Registration date Sunday July 18, 2010 Status Member Last seen July 18, 2010 - Jul 18, 2010 at 02:29 PM
 hytfg - Jul 18, 2010 at 10:00 PM
Hello,

Hi all,


I using C# .NET , vs 2008 , .net 3.5

Hi all,

several issues using files:

For me, is difficult, but I need sample code in C# for this:



1. Check if a file is in use


2. If file is in use, the name of Process that use it




For example, in my issue.


I try delete file, and I get "The process cannot access the file 'XYZ' because it is being used by another process." Exception.


File.Delete(infoFichero.Ruta);


I want check if a file is in use, and the name of Process that use it.


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).


I have read several references but not get sample code source,




How to check if a file is in use?

https://stackoverflow.com/questions/3138483/emulate-waiting-on-file-open-in-c-sharp-when-file-is-locked


https://social.msdn.microsoft.com/Forums/vstudio/en-US/9dabc172-237a-42db-850e-ada08885a5d5/help-needed-the-handle-is-invalid-when-trying-to-read-the-file-using-streamreader-from-a-network?forum=csharpgeneral

https://stackoverflow.com/questions/1314958/how-to-check-if-a-file-is-in-use


https://stackoverflow.com/questions/1389155/easiest-way-to-read-text-file-which-is-locked-by-another-application


https://stackoverflow.com/questions/698442/using-c-sharp-is-it-possible-to-test-if-a-lock-is-held-on-a-file





1. How get the error code of IOException "The process cannot access the file 'XYZ' because it is being used by another process."


For example, in my issue.


I try delete file, and I get "The process cannot access the file 'XYZ' because it is being used by another process." Exception.




try
{


File.Delete(infoFichero.Ruta);
}
catch (IOException ex)
{
// ex.Message == "The process cannot access the file 'XYZ' because it is being used by another process."
}




But if .NET is Spanish, I get "El proceso no puede obtener acceso al archivo '00000004.PDF' porque está siendo utilizado en otro" message.


System.IO.IOException: El proceso no puede obtener acceso al archivo '00000004.PDF' porque está siendo utilizado en otro proceso.
en System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
en System.IO.FileInfo.Delete()




I need a ERROR CODE for that Exception. In Trace, I have seen System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)



How get the error code of IOException "The process cannot access the file 'XYZ' because it is being used by another process."



Please, any sample code, I ask for help gurus, MVPs, anyone...


Thanks in advanced, greetings, any help will be very big fat appreciated



Related:

1 response

complicated
0