如何检查IOException是否不够 - 磁盘空间异常类型? [英] How to check if IOException is Not-Enough-Disk-Space-Exception type?
问题描述
如何检查 IOException
是否为不足够的磁盘空间异常类型?
How can I check if IOException
is a "Not enough disk space" exception type?
在我检查一下,看看邮件是否匹配没有足够的磁盘空间,但我知道如果操作系统语言不是英文,这将不起作用。
At the moment I check to see if the message matches something like "Not enough disk space", but I know that this won't work if the OS language is not English.
推荐答案
您需要检查 HResult
并针对 ERROR_DISK_FULL(0x70)和 ERROR_HANDLE_DISK_FULL(0x27),其中可以转换为 HResults
by OR
'with with $ code> 0x80070000 。
You need to check the HResult
and test against ERROR_DISK_FULL (0x70) and ERROR_HANDLE_DISK_FULL (0x27), which can be converted to HResults
by OR
'ing with 0x80070000
.
对于.Net Framework 4.5以上,您可以使用 Ex ception.HResult
属性:
For .Net Framework 4.5 and above, you can use the Exception.HResult
property:
static bool IsDiskFull(Exception ex)
{
const int HR_ERROR_HANDLE_DISK_FULL = unchecked((int)0x80070027);
const int HR_ERROR_DISK_FULL = unchecked((int)0x80070070);
return ex.HResult == HR_ERROR_HANDLE_DISK_FULL
|| ex.HResult == HR_ERROR_DISK_FULL;
}
对于旧版本,您可以使用 Marshal.GetHRForException
以获取HResult,但是这个具有重大的副作用,不推荐:
For older versions, you can use Marshal.GetHRForException
to get back the HResult, but this has significant side-effects and is not recommended:
static bool IsDiskFull(Exception ex)
{
const int ERROR_HANDLE_DISK_FULL = 0x27;
const int ERROR_DISK_FULL = 0x70;
int win32ErrorCode = Marshal.GetHRForException(ex) & 0xFFFF;
return win32ErrorCode == ERROR_HANDLE_DISK_FULL || win32ErrorCode == ERROR_DISK_FULL;
}
从MSDN文档:
请注意, GetHRForException 方法设置
当前线程的 IErrorInfo 。这可能会导致意外的结果,如
ThrowExceptionForHR 的方法,默认使用当前线程的
的 IErrorInfo (如果已设置)。
Note that the GetHRForException method sets the IErrorInfo of the current thread. This can cause unexpected results for methods like the ThrowExceptionForHR methods that default to using the IErrorInfo of the current thread if it is set.
另请参见如何确定System.IO.IOException的HResult?
这篇关于如何检查IOException是否不够 - 磁盘空间异常类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!