Click here to Skip to main content
15,890,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My application can recover from an insufficient disk space problem by switching the output file to another disk. I need to know how to recognize the specific error. This works
C#
try
{ streamWriter.WriteLine(text); }
catch (IOException io)
{
  if (ex.Message.Equals("There is not enough space on the disk.\r\n"))
    DoSomething();
}


I do not like testing for an English literal that may change in a future release and, most likely, is not very compatible with non-English machines.

What is a better way of determining the specific error?
Posted

1 solution

Have you tried looking at the HResult[^] property? I don't have a full disk to try it on, so I can;t check. It should return ERROR_DISK_FULL (0x70) or ERROR_HANDLE_DISK_FULL (0x27), but if it doesn't, you will have to use the Marshal.GetHRForException[^] method, which will.
 
Share this answer
 
Comments
Roger500 13-Oct-12 9:43am    
After doing some digging I figured out how to implement your solution. It works GREAT!
catch (IOException ex)
{ int hresult = System.Runtime.InteropServices.Marshal.GetHRForException(ex);
...
}

Many thanks!
OriginalGriff 13-Oct-12 9:46am    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900