.NET调用WMI后无论是同步调用还是异步调用,都会产生返回一个int类型的执行结果。如果成功,则返回0。如果不是0,则有对应错误码表示发生了什么错误。

根据咱们这个系列的博文,我总结了关于进程,服务,目录和共享四种异常的错误码。

CheckExceptionClass类是异常检查类,所有WMI执行后,都要执行这个类里面的相关方法进行状态检测,以确保执行成功。代码如下:

using System;
using System.Collections.Generic;
using System.Text;
using TJVictor.WMI.WmiException; namespace TJVictor.WMI
{
class CheckExceptionClass
{
public static void CheckDirectoryExcepton(int exceptionCode)
{
if (!exceptionCode.Equals())
throw new DirectoryException(exceptionCode);
} public static void CheckProcessException(int exceptionCode)
{
if (!exceptionCode.Equals())
throw new ProcessException(exceptionCode);
} public static void CheckServiceException(int exceptionCode)
{
if (!exceptionCode.Equals())
throw new ServiceException(exceptionCode);
} public static void CheckShareException(int exceptionCode)
{
//if (!exceptionCode.Equals(0))
// throw new IICV2WmiAccess.WmiException.ShareException(exceptionCode);
}
}
} ProcessException类是进程异常类。代码如下:
using System;
using System.Collections.Generic;
using System.Text;
using System.Management; namespace TJVictor.WMI.WmiException
{
///
/// The Create method creates a new process.
/// The method returns an integer value that can be interpretted as follows:
/// 0 - Successful completion.
/// 2 - The user does not have access to the requested information.
/// 3 - The user does not have sufficient privilge.
/// 8 - Unknown failure.
/// 9 - The path specified does not exist.
/// 21 - The specified parameter is invalid.
/// Other - For integer values other than those listed above, refer to Win32 error code documentation.
///
public class ProcessException : ManagementException
{
public new string Message;
public ProcessException(int errorCode)
: base()
{
switch (errorCode)
{
case : Message = "The user does not have access to the requested information."; break;
case : Message = "The user does not have sufficient privilge."; break;
case : Message = "Unknown failure."; break;
case : Message = "The path specified does not exist."; break;
case : Message = "The specified parameter is invalid."; break;
default: Message = "Unknown failure."; break;
}
} public ProcessException(string message)
: base()
{
this.Message = message;
}
}
} ServiceException类是服务异常类。代码如下:
using System;
using System.Collections.Generic;
using System.Text;
using System.Management; namespace TJVictor.WMI.WmiException
{
///
/// 0 - The request was accepted.
/// 1 - The request is not supported.
/// 2 - The user did not have the necessary access.
/// 3 - The service cannot be stopped because other services that are running are dependent on it.
/// 4 - The requested control code is not valid, or it is unacceptable to the service.
/// 5 - The requested control code cannot be sent to the service because the state of the service (Win32_BaseService:State) is equal to 0, 1, or 2.
/// 6 - The service has not been started.
/// 7 - The service did not respond to the start request in a timely fashion.
/// 8 - Unknown failure when starting the service.
/// 9 - The directory path to the service executable was not found.
/// 10 - The service is already running.
/// 11 - The database to add a new service is locked.
/// 12 - A dependency for which this service relies on has been removed from the system.
/// 13 - The service failed to find the service needed from a dependent service.
/// 14 - The service has been disabled from the system.
/// 15 - The service does not have the correct authentication to run on the system.
/// 16 - This service is being removed from the system.
/// 17 - There is no execution thread for the service.
/// 18 - There are circular dependencies when starting the service.
/// 19 - There is a service running under the same name.
/// 20 - There are invalid characters in the name of the service.
/// 21 - Invalid parameters have been passed to the service.
/// 22 - The account, which this service is to run under is either invalid or lacks the permissions to run the service.
/// 23 - The service exists in the database of services available from the system.
/// 24 - The service is currently paused in the system.
/// Other - For integer values other than those listed above, refer to Win32 error code documentation.
///
public class ServiceException : ManagementException
{
public new string Message;
public ServiceException(int errorCode)
: base()
{
switch (errorCode)
{
case : Message = "The request was accepted."; break;
case : Message = "The request is not supported."; break;
case : Message = "The user did not have the necessary access."; break;
case : Message = "The service cannot be stopped because other services that are running are dependent on it."; break;
case : Message = "The requested control code is not valid, or it is unacceptable to the service."; break;
case : Message = "The service is already stopped."; break;
case : Message = "The service has not been started."; break;
case : Message = "The service did not respond to the start request in a timely fashion."; break;
case : Message = "Unknown failure when starting the service."; break;
case : Message = "The directory path to the service executable was not found."; break;
case : Message = "The service is already running."; break;
case : Message = "The database to add a new service is locked."; break;
case : Message = "A dependency for which this service relies on has been removed from the system."; break;
case : Message = "The service failed to find the service needed from a dependent service."; break;
case : Message = "The service has been disabled from the system."; break;
case : Message = "The service does not have the correct authentication to run on the system."; break;
case : Message = "This service is being removed from the system."; break;
case : Message = "There is no execution thread for the service."; break;
case : Message = "There are circular dependencies when starting the service."; break;
case : Message = "There is a service running under the same name."; break;
case : Message = "There are invalid characters in the name of the service."; break;
case : Message = "Invalid parameters have been passed to the service."; break;
case : Message = "The account, which this service is to run under is either invalid or lacks the permissions to run the service."; break;
case : Message = "The service exists in the database of services available from the system."; break;
case : Message = "The service is currently paused in the system."; break;
default: Message = "Unknown failure."; break;
}
} public ServiceException(string message)
: base()
{
this.Message = message;
}
}
} DirectoryException类是目录异常类。代码如下:
using System;
using System.Collections.Generic;
using System.Text;
using System.Management; namespace TJVictor.WMI.WmiException
{
///
/// 0 - The request was successful.
/// 2 - Access was denied.
/// 8 - An unspecified failure occurred.
/// 9 - The name specified was invalid.
/// 10 - The object specified already exists.
/// 11 - The file system is not NTFS.
/// 12 - The platform is not Windows NT or Windows 2000.
/// 13 - The drive is not the same.
/// 14 - The directory is not empty.
/// 15 - There has been a sharing violation.
/// 16 - The start file specified was invalid.
/// 17 - A privilege required for the operation is not held.
/// 21 - A parameter specified is invalid.
///
public class DirectoryException : ManagementException
{
public new string Message;
public DirectoryException(int errorCode)
: base()
{
switch (errorCode)
{
case : Message = "Access was denied."; break;
case : Message = "An unspecified failure occurred."; break;
case : Message = "The name specified was invalid."; break;
case : Message = "The object specified already exists."; break;
case : Message = "The file system is not NTFS."; break;
case : Message = "The platform is not Windows NT or Windows 2000."; break;
case : Message = "The drive is not the same."; break;
case : Message = "The directory is not empty."; break;
case : Message = "There has been a sharing violation."; break;
case : Message = "The start file specified was invalid."; break;
case : Message = "A privilege required for the operation is not held."; break;
case : Message = "A parameter specified is invalid."; break;
default: Message = "Unknown failure."; break;
}
} public DirectoryException(string message)
: base()
{
Message = message;
}
}
} ShareException类是共享异常类。代码如下:
using System;
using System.Collections.Generic;
using System.Text;
using System.Management; namespace TJVictor.WMI.WmiException
{
///
/// 0 - Successful completion.
/// 2 - The user does not have access to the requested information.
/// 8 - Unknown failure.
/// 9 - The character or file system name is invalid.
/// 10 - The value specified for the level parameter is invalid.
/// 21 - The specified parameter is invalid.
/// 22 - The share name is already in use on this server.
/// 23 - The operation is invalid for a redirected resource. The specified device name is assigned to a shared resource.
/// 24 - The device or directory does not exist.
/// 25 - The share name does not exist.
/// Other - For integer values other than those listed above, refer to Win32 error code documentation.
///
public class ShareException : ManagementException
{
public new string Message;
public ShareException(int errorCode)
: base()
{
switch (errorCode)
{
case : Message = "The user does not have access to the requested information."; break;
case : Message = "Unknown failure."; break;
case : Message = "The character or file system name is invalid."; break;
case : Message = "The value specified for the level parameter is invalid. "; break;
case : Message = "The specified parameter is invalid."; break;
case : Message = "The share name is already in use on this server."; break;
case : Message = "The operation is invalid for a redirected resource. The specified device name is assigned to a shared resource."; break;
case : Message = "The device or directory does not exist."; break;
case : Message = "The share name does not exist."; break;
default: Message = "For integer values other than those listed above, refer to Win32 error code documentation."; break;
}
} public ShareException(string message)
: base()
{
this.Message = message;
}
}
} 关于这个类的使用方法,我在介绍使用WMI控制进程、服务、目录、共享的代码中都有体现。这里不再对其使用方法做过多的介绍。
其实异常类的错误代码可以在MSDN中找到,这里不再一一列举。

ManagementException:WMI异常处理介绍的更多相关文章

  1. WMI技术介绍和应用——查询硬件信息

    //查询得到系统盘所在硬盘的ID SELECT DiskIndex FROM Win32_DiskPartition WHERE Bootable = TRUE //如何使用WMI查询系统盘所在硬盘的 ...

  2. Java异常处理介绍(Java知识的重点内容)

    Java 异常处理 异常是程序中的一些错误,但并不是所有的错误都是异常,并且错误有时候是可以避免的. 比如说,你的代码少了一个分号,那么运行出来结果是提示是错误 java.lang.Error:如果你 ...

  3. WMI参数介绍

    Win32_DiskDrive 硬盘 参数说明 vailability  --设备的状态.BytesPerSector  --在每个扇区的物理磁盘驱动器的字节数.Capabilities  --媒体访 ...

  4. WMI技术介绍和应用——WMI概述

    https://blog.csdn.net/breaksoftware/article/details/8424317

  5. 使用Python玩转WMI

    最近在网上搜索Python和WMI相关资料时,发现大部分文章都千篇一律,并且基本上只说了很基础的使用,并未深入说明如何使用WMI.本文打算更进一步,让我们使用Python玩转WMI. 1 什么是WMI ...

  6. Spring MVC异常处理 和 重定向传递数据

    1.异常处理介绍 Spring在web项目中,如果在请求处理时出现异常,那输出会是Servlet响应.这时异常需要以某种方式转换为响应. Spring将异常转换为响应的方式: a.特定的Spring异 ...

  7. Python面试题汇总

    原文:http://blog.csdn.net/jerry_1126/article/details/44023949 拿网络上关于Python的面试题汇总了,给出了自认为合理的答案,有些题目不错,可 ...

  8. async 与 await异步编程活用基础

    [本文转自:http://www.cnblogs.com/x-xk/archive/2013/06/05/3118005.html  作者:肅] 好久没写博客了,时隔5个月,奉上一篇精心准备的文章,希 ...

  9. async And await异步编程活用基础

    原文:async And await异步编程活用基础 好久没写博客了,时隔5个月,奉上一篇精心准备的文章,希望大家能有所收获,对async 和 await 的理解有更深一层的理解. async 和 a ...

随机推荐

  1. 关于set或map的key使用自定义类型的问题

    我们都知道set或map的key使用自定义类型时必须重载<关系运算符 但是,还有一个条件,所调用重载的小于操作符,使用的对象必须是const 而对象调用的方法也必须是const的 1 #incl ...

  2. java Active Object模式(下)

    Active Object模式的评价与实现考量 Active Object模式通过将方法的调用与执行分离,实现了异步编程.有利于提高并发性,从而提高系统的吞吐率. Active Object模式还有个 ...

  3. 禁用窗体关闭按钮(使用GetWindowLong修改GWL_STYLE)

    一般我们不想让窗体能够关闭, 首先想到的是在OnCloseQuery事件里设置CanClose := False, 不过在某些情况下这个会和程序关闭窗体的业务逻辑产生冲突 所以写了下面这个函数, 可以 ...

  4. bzoj1135

    POI阴影又发作了但这道题挺好的,比较涨知识裸的想法是裸的每次二分图匹配,但显然会TLE这里就要引入Hall定理:二分图G中的两部分顶点组成的集合分别为X, Y, X={X1, X2, X3,X4,. ...

  5. CH Round #51 - Shinrein祭 #1

    A.Phorni 题目:http://www.contesthunter.org/contest/CH%20Round%20%2351%20-%20Shinrein祭%20%231/Phorni 没做 ...

  6. ubuntu下新建VPN连接

    1. 安装VPN Client#sudo apt-get install pptp-linux2. 安装网络管理器对VPN的支持#sudo apt-get install network-manage ...

  7. 【REST API】

    微信公众平台开发者文档 RESTful API 设计最佳实践 登录判断写去接口里 PUT 新建一个资源POST 更新一个资源GET 查看一个资源DELETE 删除一个资源

  8. Linux企业级开发技术(3)——epoll企业级开发之epoll模型

    EPOLL事件有两种模型: Edge Triggered (ET)  边缘触发 只有数据到来,才触发,不管缓存区中是否还有数据. Level Triggered (LT)  水平触发 只要有数据都会触 ...

  9. Selenium自动化测试环境搭建汇总(一):Selenium+Eclipse+Junit+TestNG

    第一步 安装JDK JDk1.7. 下载地址:http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-188026 ...

  10. Rotation Lock Puzzle

    Problem Description Alice was felling into a cave. She found a strange door with a number square mat ...