.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. ctr预估模型

    http://wenku.baidu.com/course/view/1488bfd5b9f3f90f76c61b8d

  2. 专家解读Linux操作系统内核中的GCC特性

    专家解读Linux操作系统内核中的GCC特性   Linux内核使用GNU Compiler Collection (GCC)套件的几个特殊功能.这些功能包括提供快捷方式和简化以及向编译器提供优化提示 ...

  3. js和jsp

    1.js中的字符串赋值可以用' '或者" ". alert('1111'); alert("1111"); document.getElementById('d ...

  4. RC隔离 更新where条件列 没有索引的情况

    CREATE TABLE `test100` ( `sn` int(11) NOT NULL AUTO_INCREMENT COMMENT '自增编号', `phoneNo` int(11) , `c ...

  5. Round Numbers (排列组合)

    Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7558   Accepted: 2596 Description The c ...

  6. 「Poetize3」导弹防御塔

    描述 Description Freda控制着N座可以发射导弹的防御塔.每座塔都有足够数量的导弹,但是每座塔每次只能发射一枚.在发射导弹时,导弹需要T1秒才能从防御塔中射出,而在发射导弹后,发射这枚导 ...

  7. JavaScript框架设计 第14章 动画引擎

    easing-js <!DOCTYPE html> <html> <head> <meta charset="utf-8"> < ...

  8. 【转】Any way to implement BLE notifications in Android-L preview----不错

    原文网址:http://stackoverflow.com/questions/24865120/any-way-to-implement-ble-notifications-in-android-l ...

  9. 【转】android ListView详解---- 不错不错

    原文网址:http://www.cnblogs.com/allin/archive/2010/05/11/1732200.html 由于google doc 很多人都打不开,故更新了源码下载地址 [源 ...

  10. 【模拟】BAPC2014 G Growling Gears (Codeforces GYM 100526)

    题目链接: http://codeforces.com/gym/100526 http://acm.hunnu.edu.cn/online/?action=problem&type=show& ...