I have a C# application and I want to copy a file to a new location. Some times I need to overwrite an existing file. when this happens I receive a System.IO.IOException. I want to recover from a Sharing violation but how do I determine that IOException was returned because the destination file is in use rather then some other reason? I could look for the "The process cannot access the file because it is being used by another process." message... But I don't like that idea.
This article explains how to get the exception HRESULT so you can determine the error cause.
-
No, IOException uses the HRESULT COR_E_IO which has the constant value 0x80131620 see IOException Class. – tibx Feb 19 '18 at 14:14
-
but in fact it seems that HResult property of IOException class has the real HRSEULT error reason... – tibx Feb 19 '18 at 14:22
-
1@tibx it was said but it's not true. Tried
FileInfo.MoveTo
and theIOException
s have differentHResult
by moving to an existing file/dir or moving to a drive letter (`d:\`). – Meow Cat 2012 Aug 27 '19 at 4:25
This was the solution I came up with.
private void RobustMoveFile( System.IO.DirectoryInfo destinationDirectory, System.IO.FileInfo sourceFile, Boolean retryMove )
{
try
{
string DestinationFile = Path.Combine( destinationDirectory.FullName, sourceFile.Name );
if ( File.Exists( DestinationFile ) )
sourceFile.Replace( DestinationFile, DestinationFile + "Back", true );
else
{
sourceFile.CopyTo( DestinationFile, true );
sourceFile.Delete();
}
}
catch ( System.IO.IOException IOEx )
{
int HResult = System.Runtime.InteropServices.Marshal.GetHRForException( IOEx );
const int SharingViolation = 32;
if ( ( HResult & 0xFFFF ) == SharingViolation && retryMove )
RobustMoveFile( destinationDirectory, sourceFile, false );
throw;
}
}
-
1No need to check for destination file existence, as some other process may create/delete it between the check and the replace/CopyTo operations. Just change your logic that you always create the detination file no matter what, and if this fails, recover or retry. – Sunny Milenov Jan 9 '09 at 15:12
As other answers have stated, you need to get the HResult of the error and check it. An HResult of 32 is a sharing violation.
In .NET 4.5, the IOException
has a public HResult
property, so you can just do as follows:
try
{
// do file IO here
}
catch (IOException e)
{
if (e.HResult == 32) // 32 = Sharing violation
{
// Recovery logic goes here
}
else
{
throw; // didn't need to catch this
}
}
In earlier versions of .NET, however, you need to get the HResult
by calling Marshal.GetHRForException(Exception)
, so the similar code would be:
try
{
// do file IO here
}
catch (IOException e)
{
int HResult = System.Runtime.InteropServices.Marshal.GetHRForException(e)
if (HResult == 32) // 32 = Sharing violation
{
// Recovery logic goes here
}
else
{
throw; // Or do whatever else here
}
}
The not-yet released C# 6.0 will, according to A C# 6.0 Language Preview, allow you to use this syntax to catch only a sharing violation:
try
{
// do file IO here
}
catch (IOException e)
if (e.HResult == 32) // 32 = Sharing violation
{
// Recovery logic goes here
}
-
are you sure this is entirely correct? experimenting with this, I think you need to bitwise and (&&) the HResult with 0x000000000000FFFF. So you want to check (0xFFFF && e.HResult)==32 – Greylander Apr 10 '15 at 18:58
-
@Greylander I think you are right about the requirement to do the bitwise and, but the operator is "&", not "&&". The latter is the logical short-circuit "and". BTW here is a reference for converting a W32 error to an HRESULT. So the purpose of the bitwise-and is to do the reverse. – drwatsoncode Mar 7 '16 at 14:39
Look for the explicit error codes that you can deal with, for example:
catch (Exception u) { if (((SocketException)u).ErrorCode == 10035) ...
Take a look here: http://msdn.microsoft.com/en-us/library/ms681391(VS.85).aspx
for error codes, e.g.:
ERROR_SHARING_VIOLATION - 32 - 0x20
ERROR_ACCESS_DENIED = 5 - 0x5
ERROR_FILE_NOT_FOUND - 2 - 0x2
-
How will the cast from IOException to SocketException will succeed? – Sunny Milenov Jan 8 '09 at 21:53
-