ManagementException:WMI异常处理介绍
.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异常处理介绍的更多相关文章
- WMI技术介绍和应用——查询硬件信息
//查询得到系统盘所在硬盘的ID SELECT DiskIndex FROM Win32_DiskPartition WHERE Bootable = TRUE //如何使用WMI查询系统盘所在硬盘的 ...
- Java异常处理介绍(Java知识的重点内容)
Java 异常处理 异常是程序中的一些错误,但并不是所有的错误都是异常,并且错误有时候是可以避免的. 比如说,你的代码少了一个分号,那么运行出来结果是提示是错误 java.lang.Error:如果你 ...
- WMI参数介绍
Win32_DiskDrive 硬盘 参数说明 vailability --设备的状态.BytesPerSector --在每个扇区的物理磁盘驱动器的字节数.Capabilities --媒体访 ...
- WMI技术介绍和应用——WMI概述
https://blog.csdn.net/breaksoftware/article/details/8424317
- 使用Python玩转WMI
最近在网上搜索Python和WMI相关资料时,发现大部分文章都千篇一律,并且基本上只说了很基础的使用,并未深入说明如何使用WMI.本文打算更进一步,让我们使用Python玩转WMI. 1 什么是WMI ...
- Spring MVC异常处理 和 重定向传递数据
1.异常处理介绍 Spring在web项目中,如果在请求处理时出现异常,那输出会是Servlet响应.这时异常需要以某种方式转换为响应. Spring将异常转换为响应的方式: a.特定的Spring异 ...
- Python面试题汇总
原文:http://blog.csdn.net/jerry_1126/article/details/44023949 拿网络上关于Python的面试题汇总了,给出了自认为合理的答案,有些题目不错,可 ...
- async 与 await异步编程活用基础
[本文转自:http://www.cnblogs.com/x-xk/archive/2013/06/05/3118005.html 作者:肅] 好久没写博客了,时隔5个月,奉上一篇精心准备的文章,希 ...
- async And await异步编程活用基础
原文:async And await异步编程活用基础 好久没写博客了,时隔5个月,奉上一篇精心准备的文章,希望大家能有所收获,对async 和 await 的理解有更深一层的理解. async 和 a ...
随机推荐
- OrCAD PSpice仿真库模型
说明:本介绍包含了\capture\library\pspice和capture\library\pspice\advanls目录下所有库,但由于作者水平有限,介绍得也比较简单,有些说明可能不一定对. ...
- (转载)50个c/c++源代码网站
C/C++是最主要的编程语言.这里列出了50名优秀网站和网页清单,这些网站提供c/c++源代码.这份清单提供了源代码的链接以及它们的小说明.我已 尽力包括最佳的C/C++源代码的网站.这不是一个完整的 ...
- VC版本的MakeObjectInstance把WNDPROC映射到类的成员函数
这段时间用VC封装Windows类库,没有MakeObjectInstance处理窗口消息确实不爽,又不想使用MFC的消息映射,这玩意的效率和美观只能呵呵. 至于MakeObjectInstance是 ...
- 4. Repeater 实例2
设计管理一个用户程序,对用户的状态进行管理,当用户状态是启用时整行显示红色. 设计思路:用Repeater遍历每行记录,在操作状态的表格中旋转两个按钮,一个为启用功能,另一个为禁用功能,根据Repea ...
- width:auto; 和 width:100%;的不同
width:auto:会将元素撑开至整个父元素width,但是会减去子节点自己的margin,padding或者border的大小.width:100%:会强制将元素变成和父元素一样的宽,并且添加额外 ...
- ZOJ-2112-Dynamic Rankings(线段树套splay树)
题意: 完成两个操作: 1.询问一个区间里第k小的数: 2.修改数列中一个数的值. 分析: 线段树套平衡树,线段树中的每个节点都有一棵平衡树,维护线段树所记录的这个区间的元素.这样处理空间上是O(nl ...
- hust-1024-dance party(最大流--枚举,可行流判断)
题意: 舞会上,男孩和女孩配对,求最大完全匹配个数,要求每个人最多与k个不喜欢的人配对,且每次都和不同的人配对. 分析: 将一个点拆成3个点. b, b1, b2. 从1到n枚举ans, 判可 ...
- hihoCoder 1392 War Chess 【模拟】 (ACM-ICPC国际大学生程序设计竞赛北京赛区(2016)网络赛)
#1392 : War Chess 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 Rainbow loves to play kinds of War Chess gam ...
- 【离线】【深搜】【树】Codeforces 707D Persistent Bookcase
题目链接: http://codeforces.com/problemset/problem/707/D 题目大意: 一个N*M的书架,支持4种操作 1.把(x,y)变为有书. 2.把(x,y)变为没 ...
- linq里的select和selectmany操作 投影运算
原文地址:https://msdn.microsoft.com/zh-cn/library/bb546168.aspx#Mtps_DropDownFilterText 投影运算 其他版本 投影 ...