zoukankan      html  css  js  c++  java
  • silverlight 4.0 的oob模式下,调用com通过wmi重启自身进程 killself

    silverlight目前开发的应用,想做到系统内注销后自动重新启动下 sllauncher.exe ,实现方式是通过WMI的COM接口,获取到当前应用的执行命令行(CommandLine);并通过shell运行;代码如下:

    #region Using Section
    
    using System;
    using System.Collections.Generic;
    using System.Runtime.InteropServices.Automation;
    using System.Windows;
    
    #endregion
    
    namespace KillSelf
    {
        public class ComObjectHelper
        {
            private static List<string> GetProcess()
            {
                var result = new List<string>();
                try
                {
                    dynamic objLocator = AutomationFactory.CreateObject("WbemScripting.SWbemLocator");
                   
                    dynamic objWmiService = objLocator.ConnectServer(".", "root\cimv2");
    
                    dynamic query =
                        objWmiService.ExecQuery("Select * from Win32_Process");
    
                    foreach (dynamic o in query)
                    {
                        //string value = "ExecutablePath = " + o.CommandLine + "
    ";
                        //Console.WriteLine(value);
    
                        result.Add(o.CommandLine + "");
                    }
    
                    return result;
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Get Process:" + ex.Message);
                }
                return null;
            }
    
            internal static void Exec(string cmdline)
            {
                try
                {
                    dynamic cmd = AutomationFactory.CreateObject("WScript.Shell");
                    cmd.Run(cmdline, 1, false);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Execute Process:" + ex.Message);
                }
            }
    
            public static SlProcess GetSelf()
            {
                string[] procs = GetProcess().ToArray();
    
                foreach (string p in procs)
                {
                    string cmdline = p.ToLower();
                    if (cmdline.IndexOf("sllauncher.exe", StringComparison.Ordinal) != -1)
                    {
                        return new SlProcess(cmdline);
                    }
                }
                throw new NullReferenceException("未找到SLLauncher.exe进程");
            }
        }
    
        public class SlProcess
        {
            public SlProcess(string cmdline)
            {
                CommandLine = cmdline;
            }
    
            public string CommandLine { get; set; }
    
            public SlProcess Run()
            {
                ComObjectHelper.Exec(CommandLine);
                return this;
            }
    
            public SlProcess Kill()
            {
                Application.Current.MainWindow.Close();
                return this;
            }
        }
    }

    调用部分:

            private void Button_Click(object sender, RoutedEventArgs e)
            {
                if (!Application.Current.IsRunningOutOfBrowser)
                {
                    MessageBox.Show("Not Running OutOfBrowser!!");
                    return;
                }
                
                ComObjectHelper.GetSelf().Run().Kill();
            }
    
  • 相关阅读:
    在不打开excel的情况下用python执行excel
    Python中xlrd、xlwt、win32com模块对xls文件的读写操作
    [已解决]报错:have mixed types. Specify dtype option on import or set low_memory=False
    [已解决]报错:xlrd.compdoc.CompDocError: Workbook: size exceeds expected 17920 bytes; corrupt?
    使用Pandas读取大型Excel文件
    [转]jmeter实战
    webService(SOAP)性能测试脚本
    jmeter正则表达式提取器--关联
    Data Set Config配置元件
    压力测试涉及到的参数
  • 原文地址:https://www.cnblogs.com/Chinasf/p/3781348.html
Copyright © 2011-2022 走看看