zoukankan      html  css  js  c++  java
  • 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(0))
                    throw new DirectoryException(exceptionCode);
            }
    
            public static void CheckProcessException(int exceptionCode)
            {
                if (!exceptionCode.Equals(0))
                    throw new ProcessException(exceptionCode);
            }
    
            public static void CheckServiceException(int exceptionCode)
            {
                if (!exceptionCode.Equals(0))
                    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 2: Message = "The user does not have access to the requested information."; break;
                    case 3: Message = "The user does not have sufficient privilge."; break;
                    case 8: Message = "Unknown failure."; break;
                    case 9: Message = "The path specified does not exist."; break;
                    case 21: 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 0: Message = "The request was accepted."; break;
                    case 1: Message = "The request is not supported."; break;
                    case 2: Message = "The user did not have the necessary access."; break;
                    case 3: Message = "The service cannot be stopped because other services that are running are dependent on it."; break;
                    case 4: Message = "The requested control code is not valid, or it is unacceptable to the service."; break;
                    case 5: Message = "The service is already stopped."; break;
                    case 6: Message = "The service has not been started."; break;
                    case 7: Message = "The service did not respond to the start request in a timely fashion."; break;
                    case 8: Message = "Unknown failure when starting the service."; break;
                    case 9: Message = "The directory path to the service executable was not found."; break;
                    case 10: Message = "The service is already running."; break;
                    case 11: Message = "The database to add a new service is locked."; break;
                    case 12: Message = "A dependency for which this service relies on has been removed from the system."; break;
                    case 13: Message = "The service failed to find the service needed from a dependent service."; break;
                    case 14: Message = "The service has been disabled from the system."; break;
                    case 15: Message = "The service does not have the correct authentication to run on the system."; break;
                    case 16: Message = "This service is being removed from the system."; break;
                    case 17: Message = "There is no execution thread for the service."; break;
                    case 18: Message = "There are circular dependencies when starting the service."; break;
                    case 19: Message = "There is a service running under the same name."; break;
                    case 20: Message = "There are invalid characters in the name of the service."; break;
                    case 21: Message = "Invalid parameters have been passed to the service."; break;
                    case 22: Message = "The account, which this service is to run under is either invalid or lacks the permissions to run the service."; break;
                    case 23: Message = "The service exists in the database of services available from the system."; break;
                    case 24: 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 2: Message = "Access was denied."; break;
                    case 8: Message = "An unspecified failure occurred."; break;
                    case 9: Message = "The name specified was invalid."; break;
                    case 10: Message = "The object specified already exists."; break;
                    case 11: Message = "The file system is not NTFS."; break;
                    case 12: Message = "The platform is not Windows NT or Windows 2000."; break;
                    case 13: Message = "The drive is not the same."; break;
                    case 14: Message = "The directory is not empty."; break;
                    case 15: Message = "There has been a sharing violation."; break;
                    case 16: Message = "The start file specified was invalid."; break;
                    case 17: Message = "A privilege required for the operation is not held."; break;
                    case 21: 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 2: Message = "The user does not have access to the requested information."; break;
                    case 8: Message = "Unknown failure."; break;
                    case 9: Message = "The character or file system name is invalid."; break;
                    case 10: Message = "The value specified for the level parameter is invalid. "; break;
                    case 21: Message = "The specified parameter is invalid."; break;
                    case 22: Message = "The share name is already in use on this server."; break;
                    case 23: Message = "The operation is invalid for a redirected resource. The specified device name is assigned to a shared resource."; break;
                    case 24: Message = "The device or directory does not exist."; break;
                    case 25: 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中找到,这里不再一一列举。
  • 相关阅读:
    去除bootstrap默认的input和选中时的样式
    js生成二维码
    内网访问已经启动的vue项目
    vue项目未加载完成前显示loading...
    node通过QQ邮箱发送邮件
    Ubuntu 使用crontab做定时任务
    mysqldump 使用及其注意事项
    八大排序之选择类排序
    javax.sound.sampled.AudioInputStream
    java.io.StringBufferInputStream
  • 原文地址:https://www.cnblogs.com/tianma3798/p/3560620.html
Copyright © 2011-2022 走看看