2

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

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

  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 proceso" 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."

Kiquenet
  • 14,494
  • 35
  • 148
  • 243
  • 1
    @John, FAQ does not say to leave out Hellos and Thankses.. Being polite doesn't cost anything and can't believe it would be anything other than considerate? – Kieren Johnstone Jul 15 '10 at 20:31
  • 1
    @Kieren: see [Should ‘Hi’, ‘thanks’ and taglines and salutations be removed from posts?](http://meta.stackexchange.com/questions/2950/should-hi-thanks-and-taglines-and-salutations-be-removed-from-posts). – John Saunders Jul 15 '10 at 21:05
  • information on getting the process id/name after identifying the error: https://github.com/dotnet/runtime/issues/22775 https://stackoverflow.com/questions/183925/what-win32-api-can-be-used-to-find-the-process-that-has-a-given-file-open – NiKiZe Aug 29 '20 at 14:31

4 Answers4

10

You might have noticed that the HResult property is not accessible. The workaround is to use the Marshal.GetLastWin32Error() method to get the native Windows error code. Like this:

        catch (IOException ex) {
            int err = System.Runtime.InteropServices.Marshal.GetLastWin32Error();
            if (err == 32) Console.WriteLine("It's locked");
            // etc..
        }

Error code 32 is named ERROR_SHARING_VIOLATION in the SDK.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
1

there's an HResult property on (IO-)Exception that contains an error code. According to this list the error code for your exception should be 0x20 (I didn't try that though). Hope that helps.

andyp
  • 6,229
  • 3
  • 38
  • 55
0

(marked CW because this is really just an extended comment)

Why do you need the error code?

  • Are you going to take a different action based on one code versus another code?
  • What will you do if Windows or .NET changes, so that you're suddenly getting a different error code back for the same problem?
  • What do you want to do if you can't delete the same file, but for a different reason? In fact, maybe your new problem won't even throw an IOException.
John Saunders
  • 160,644
  • 26
  • 247
  • 397
  • 1
    I need code for identify error and I would like get the name of process (it is being used by another process). – Kiquenet Dec 09 '10 at 18:59
  • You should not have any need to "identify the error". What are you going to do with that information? Also, you will never know which process has the file open. Windows does not supply that information. Please tell us what you need to _accomplish_, and maybe we can help. – John Saunders Dec 09 '10 at 22:17
  • 1
    I would like identify the error. If I cannot delete a file because another process use it, I would like if is possible get the name of process that use the file, and warning (or send mail) to the user of my application. My Windows Service move, delete, a high volume of files, and I need full control about all files and process. – Kiquenet Dec 22 '10 at 09:46
  • @alhambra: you will never have full control, and you will never be told which process has the file open. The information doesn't exist. – John Saunders Dec 22 '10 at 19:02
  • Tools like Unlocker can get that information (I think), but those tools use C++ perhaps, programming low level than C#. Windows too: My Computer -> Manage -> System tools -> Shared Folders -> Open files. – Kiquenet Dec 29 '10 at 07:43
  • 1
    @alham: those tools might work for files on a Share, because there is software that has to keep track of such files. But you won't get that information in general. Note that none of the answers are about how to know which file is meant. – John Saunders Dec 29 '10 at 09:40
  • The error code is needed to distinguish for example a "file is locked" vs a "disk is full" error. And since quite a while back the information for which process that has locked the file is available, even tho not in the exception. – NiKiZe Aug 14 '20 at 11:57
  • Read what I wroge, @NiKiZe. What action are you going to take in the code, based on the error code. If you don't have a piece of code that looks at the error code and takes different actions, then you don't need an error code. If you need a human to take different actions, then use a different message string. – John Saunders Aug 28 '20 at 21:17
  • code is accessing a file, and that fails. that will ALWAYS give you a `IOException` depending on the reason you will have different `HRESULT` those [will NOT CHANGE on windows since it is from a Win32 API](https://learn.microsoft.com/en-us/windows/win32/debug/system-error-codes--0-499-). and yes if the file is locked you can retry, if disk is full you can take DIFFERENT ACTION, and if disk is unavailable (UNC Path) you can do something else, and so on. So after carefully reading this answer, and authors comments several times, I claim it to be making invalid points. – NiKiZe Aug 29 '20 at 14:22
-1

Have a look at the HRESULT property of the IOException class. This should return the Win32 HRESULT of the operation (which is what I think you're looking for?).

CodingGorilla
  • 19,612
  • 4
  • 45
  • 65