private void SetUIEnable(string serviceName)
 {
     System.Management.ManagementObject     wmi = new System.Management.ManagementObject(string.Format("Win32_Service.Name='{0}'", serviceName));
     System.Management.ManagementBaseObject cm  = wmi.GetMethodParameters("Change");
     cm["DesktopInteract"] = true;
     System.Management.ManagementBaseObject ut = wmi.InvokeMethod("Change", cm, null);
 }
Exemple #2
0
        /// <summary>
        /// 为指定的目录开启共享设置。
        /// </summary>
        /// <param name="sre">共享配置节</param>
        public void CreateShare(Share sre)
        {
            System.Management.ManagementClass      mc       = new System.Management.ManagementClass("win32_share");
            System.Management.ManagementBaseObject inParams = mc.GetMethodParameters("Create");
            inParams["Path"] = sre.Path;
            inParams["Name"] = sre.Name;
            inParams["Type"] = sre.Type;
            if (sre.MaximumAllowed == 0)
            {
                inParams["MaximumAllowed"] = null;      //null = 用户数连接无限制,否则指定一个正整数
            }
            else
            {
                inParams["MaximumAllowed"] = sre.MaximumAllowed;      //null = 用户数连接无限制,否则指定一个正整数
            }
            inParams["Description"] = sre.Description;
            inParams["Password"]    = null;
            inParams["Access"]      = null; //null = 使Everyone拥有完全控制权限

            System.Management.ManagementBaseObject outParams = mc.InvokeMethod("Create", inParams, null);
            switch ((uint)outParams.Properties["ReturnValue"].Value)
            {
            case 0:     //Success
                break;

            case 2:     //Access denied
                throw new System.Exception("无权访问");

            case 8:     //Unknown failure
                throw new System.Exception("未知失败");

            case 9:     //Invalid name
                throw new System.Exception("非法的共享名");

            case 10:     //Invalid level
                throw new System.Exception("非法的层次");

            case 21:     //Invalid parameter
                throw new System.Exception("非法的参数");

            case 22:     //Duplicate share
                throw new System.Exception("重复共享");

            case 23:     //Redirected path
                throw new System.Exception("重定向路径");

            case 24:     //Unknown device or directory
                throw new System.Exception("未知的目录");

            case 25:     //Net name not found
                throw new System.Exception("网络名不存在");

            default:
                throw new System.Exception("未知异常信息");
            }
        }
Exemple #3
0
        public static Win32API.ProcessReturnCode StartProcess(string remotemachine, string commandline)
        {
            WmiAccess acc = new WmiAccess("Win32_Process", remotemachine);

            System.Management.ManagementBaseObject inParams = acc.WMI_Class_Instance.GetMethodParameters("Create");
            inParams["CommandLine"] = commandline;
            System.Management.ManagementBaseObject rt = acc.WMI_Class_Instance.InvokeMethod("Create", inParams, null);

            Win32API.ProcessReturnCode p_out = (Win32API.ProcessReturnCode) int.Parse(rt["ReturnValue"].ToString());
            return(p_out);
        }
Exemple #4
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="wmiObj"></param>
 /// <param name="propertyName"></param>
 /// <returns></returns>
 public static T TryGetProperty <T>(this System.Management.ManagementBaseObject wmiObj, string propertyName)
 {
     try
     {
         return((T)wmiObj.GetPropertyValue(propertyName));
     }
     catch (System.Management.ManagementException)
     {
         //
     }
     return(default);
Exemple #5
0
        public static string TryGetProperty(this System.Management.ManagementBaseObject wmiObj, string propertyName)
        {
            string retval;

            try
            {
                var p = wmiObj.GetPropertyValue(propertyName);
                retval = p != null?p.ToString() : string.Empty;
            }
            catch (System.Management.ManagementException)
            {
                retval = string.Empty;
            }
            return(retval);
        }
 protected override void OnAfterInstall(IDictionary savedState)
 {
     try
     {
         base.OnAfterInstall(savedState);
         System.Management.ManagementObject myService = new System.Management.ManagementObject(
             string.Format("Win32_Service.Name='{0}'", this.serviceInstaller1.ServiceName));
         System.Management.ManagementBaseObject changeMethod = myService.GetMethodParameters("Change");
         changeMethod["DesktopInteract"] = true;//允许服务与桌面交互
         System.Management.ManagementBaseObject OutParam = myService.InvokeMethod("Change", changeMethod, null);
     }
     catch (Exception)
     {
     }
 }
 private void vInitializeRegistryToInteractWithDesktop()
 {
     try
     {
         System.Management.ConnectionOptions coOptions = new System.Management.ConnectionOptions();
         coOptions.Impersonation = System.Management.ImpersonationLevel.Impersonate;
         System.Management.ManagementScope mgmtScope = new System.Management.ManagementScope(@"root\CIMV2", coOptions);
         mgmtScope.Connect();
         System.Management.ManagementObject wmiService;
         wmiService = new System.Management.ManagementObject("Win32_Service.Name='" + mdlConstantes.clsConstantes.WINDOWSSERVICE_SISCOMENSAGEM_NAME + "'");
         System.Management.ManagementBaseObject InParam = wmiService.GetMethodParameters("Change");
         InParam["DesktopInteract"] = true;
         System.Management.ManagementBaseObject OutParam = wmiService.InvokeMethod("Change", InParam, null);
     }
     catch
     {
         // Cant access the registry
     }
 }
Exemple #8
0
 /// <summary>/// 「通常使うプリンタ」に設定する/// </summary>
 /// <param name="printerName">プリンタ名</param>
 public static void SetDefaultPrinter(string printerName)
 {
     System.Management.ManagementObjectSearcher mos =
         new System.Management.ManagementObjectSearcher(
             "Select * from Win32_Printer");
     System.Management.ManagementObjectCollection moc = mos.Get();
     //プリンタを列挙する
     foreach (System.Management.ManagementObject mo in moc)
     {
         if (((string)mo["Name"]) == printerName)
         {
             //名前を見つけたとき、デフォルトプリンタに設定する
             System.Management.ManagementBaseObject mbo =
                 mo.InvokeMethod("SetDefaultPrinter", null, null);
             if (((uint)mbo["returnValue"]) != 0)
             {
                 throw new Exception("失敗しました。");
             }
         }
     }
 }
Exemple #9
0
        public static void SetNewDefaultPrinter(string newDefaultPrinterName)
        {
            string selectAllQuery = "SELECT * FROM Win32_Printer";

            System.Management.ObjectQuery oq = new System.Management.ObjectQuery(selectAllQuery);

            System.Management.ManagementObjectSearcher   query1           = new System.Management.ManagementObjectSearcher(oq);
            System.Management.ManagementObjectCollection queryCollection1 = query1.Get();
            System.Management.ManagementObject           newDefault       = null;

            foreach (System.Management.ManagementObject mo in queryCollection1)
            {
                System.Management.PropertyDataCollection pdc = mo.Properties;
                if (mo["Name"].ToString().ToUpper().Trim() == newDefaultPrinterName.ToUpper())
                {
                    newDefault = mo;
                    break;
                }
            }

            if (newDefault != null)
            {
                System.Management.ManagementBaseObject outParams = newDefault.InvokeMethod("SetDefaultPrinter", null, null);
                string returnValue = outParams["returnValue"].ToString();
                if (returnValue != "0")
                {
                    throw new ApplicationException(string.Format("Change Printer '{0}' failed. Return Value: {1}", newDefault.ToString(), returnValue));
                }

                var     HWND_BROADCAST   = new IntPtr(0xffff);
                uint    WM_SETTINGCHANGE = 0x001A;
                UIntPtr innerPinvokeResult;
                var     pinvokeResult = SendMessageTimeout(HWND_BROADCAST, WM_SETTINGCHANGE, UIntPtr.Zero,
                                                           IntPtr.Zero, SendMessageTimeoutFlags.SMTO_NORMAL, 1000, out innerPinvokeResult);
            }
            else
            {
                throw new ApplicationException(string.Format("Change Printer '{0}' failed. Managemengt object not found", newDefaultPrinterName));
            }
        }
Exemple #10
0
        /// <summary>
        /// 取消共享指定目录。
        /// </summary>
        /// <param name="ShareName">共享名称。</param>
        public void DeleteShare(string ShareName)
        {
            System.Management.ManagementObject     classInstance = new System.Management.ManagementObject(@"root\CIMV2", "Win32_Share.Name='" + ShareName + @"'", null);
            System.Management.ManagementBaseObject outParams     = classInstance.InvokeMethod("Delete", null, null);
            switch ((uint)outParams.Properties["ReturnValue"].Value)
            {
            case 0:     //Success
                break;

            case 2:     //Access denied
                throw new System.Exception("无权访问");

            case 8:     //Unknown failure
                throw new System.Exception("未知失败");

            case 9:     //Invalid name
                throw new System.Exception("非法的共享名");

            case 10:     //Invalid level
                throw new System.Exception("非法的层次");

            case 21:     //Invalid parameter
                throw new System.Exception("非法的参数");

            case 22:     //Duplicate share
                throw new System.Exception("重复共享");

            case 23:     //Redirected path
                throw new System.Exception("重定向路径");

            case 24:     //Unknown device or directory
                throw new System.Exception("未知的目录");

            case 25:     //Net name not found
                throw new System.Exception("网络名不存在");

            default:
                throw new System.Exception("未知异常信息");
            }
        }
Exemple #11
0
        //parameterized constructor. Takes managementBaseObject
        public Device(System.Management.ManagementBaseObject usbDevice)
        {
            List <string> deviceProperties = new List <string>();

            foreach (var property in usbDevice.Properties)
            {
                //System.Diagnostics.Debug.WriteLine("Test to see CompatibleID string: " + usbDevice.GetPropertyValue("CompatibleID").ToString());

                //Cycles through properties of usbDevice and stores values in a list. If an exception occurs, the item in list is set as null
                try
                {
                    if (property.Value != null)
                    {
                        deviceProperties.Add(property.Value.ToString());
                    }
                    else
                    {
                        deviceProperties.Add(null);
                    }
                }
                catch (Exception e)
                {
                    deviceProperties.Add(null);
                }
                finally
                {
                }
            }

            //assignments are made using data from list. Looks disgusting but was the only way I could think to make the assignments. Order of list will be consistent from usbDevice properties
            name        = deviceProperties[15];
            pNPClass    = deviceProperties[16];
            pNPDeviceID = deviceProperties[17];

            hasSearchableSubstring = AuthenticateSubstring();
        }
Exemple #12
0
 public static string GetValue(this System.Management.ManagementBaseObject obj, string key, string defaultValue = null)
 {
 }
Exemple #13
0
 public static long GetLongValue(this System.Management.ManagementBaseObject obj, string key)
 {
 }
 public Logicaldisk(System.Management.ManagementObject theObject)
 {
     if ((CheckIfProperClass(theObject) == true))
     {
         PrivateLateBoundObject = theObject;
         PrivateSystemProperties = new ManagementSystemProperties(PrivateLateBoundObject);
         curObj = PrivateLateBoundObject;
     }
     else
     {
         throw new System.ArgumentException("Klassenname stimmt nicht überein.");
     }
 }
 public Logicaldisk(System.Management.ManagementBaseObject theObject)
 {
     if ((CheckIfProperClass(theObject) == true))
     {
         embeddedObj = theObject;
         PrivateSystemProperties = new ManagementSystemProperties(theObject);
         curObj = embeddedObj;
         isEmbedded = true;
     }
     else
     {
         throw new System.ArgumentException("Klassenname stimmt nicht überein.");
     }
 }
 public ManagementSystemProperties(System.Management.ManagementBaseObject ManagedObject)
 {
     PrivateLateBoundObject = ManagedObject;
 }
 public Logicaldisk(System.Management.ManagementScope mgmtScope, System.Management.ManagementPath path, System.Management.ObjectGetOptions getOptions)
 {
     if ((path != null))
     {
         if ((CheckIfProperClass(mgmtScope, path, getOptions) != true))
         {
             throw new System.ArgumentException("Klassenname stimmt nicht überein.");
         }
     }
     PrivateLateBoundObject = new System.Management.ManagementObject(mgmtScope, path, getOptions);
     PrivateSystemProperties = new ManagementSystemProperties(PrivateLateBoundObject);
     curObj = PrivateLateBoundObject;
 }