如何检查是否IOException异常不-足够磁盘空间,异常类型? [英] How to check if IOException is Not-Enough-Disk-Space-Exception type?

查看:412
本文介绍了如何检查是否IOException异常不-足够磁盘空间,异常类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何检查是否 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 和测试href="http://msdn.microsoft.com/en-us/library/windows/desktop/ms681382%28v=vs.85%29.aspx#error_disk_full">ERROR_DISK_FULL (0x70)和<一href="http://msdn.microsoft.com/en-us/library/windows/desktop/ms681382%28v=vs.85%29.aspx#error_handle_disk_full">ERROR_HANDLE_DISK_FULL (0x27)

You need to check the HResult and test against ERROR_DISK_FULL (0x70) and ERROR_HANDLE_DISK_FULL (0x27)

要获得HResult的一个例外,使用<一个href="http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.marshal.gethrforexception.aspx">Marshal.GetHRForException

To get the HResult for an exception use Marshal.GetHRForException

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;
}

注意 GetHRForException 有副作用,从MSDN文档:

Note that GetHRForException has a side effect, from the MSDN documentation:

注意 GetHRForException 方式设置的IErrorInfo   当前线程。这可能会导致意想不到的结果,如方法   <一href="http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.marshal.throwexceptionforhr.aspx">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.

另请参见<一个href="http://stackoverflow.com/questions/991537/how-do-i-determine-the-hresult-for-a-system-io-ioexception">How我确定HResult的一个System.IO.IOException?

这篇关于如何检查是否IOException异常不-足够磁盘空间,异常类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆