ManagementException:WMI异常处理介绍

时间:2022-12-28 15:57:19

.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中找到,这里不再一一列举。