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 + "\r\n";
                    //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;
        }
    }
}
复制代码

调用部分:

1
2
3
4
5
6
7
8
9
10
private  void  Button_Click( object  sender, RoutedEventArgs e)
{
     if  (!Application.Current.IsRunningOutOfBrowser)
     {
         MessageBox.Show( "Not Running OutOfBrowser!!" );
         return ;
     }
     
     ComObjectHelper.GetSelf().Run().Kill();
}

 




本文转自suifei博客园博客,原文链接:http://www.cnblogs.com/Chinasf/p/3781348.html,如需转载请自行联系原作者

相关文章
|
26天前
|
XML Go 数据格式
Windows自定义后台进程并设置为开机启动
可以在`Windows`上配置任意一个可执行文件后台启动,并且设置为开机启动。
Windows自定义后台进程并设置为开机启动
|
7月前
|
存储 固态存储 Windows
在 Windows 中,当一个应用程序窗口被关闭,该应用程序将会保留在哪里?
在 Windows 中,当一个应用程序窗口被关闭,该应用程序将会保留在哪里?
91 0
|
9月前
|
Windows
用Windows版本系统使用YiLu代理出现无法启动或无法正常使用时该怎么办?
用Windows版本系统使用YiLu代理出现无法启动或无法正常使用时该怎么办?
|
Linux PHP Windows
|
安全 Windows
vb得到一个进程的启动参数?
vb得到一个进程的启动参数?
131 0
|
C++ Windows
windows启动单个进程实例(系统中只有一个运行实例)
windows启动单个进程实例(系统中只有一个运行实例)
185 0
windows启动单个进程实例(系统中只有一个运行实例)
|
Windows
如何关闭windows server2003 服务器系统的自动更新功能
如何关闭windows server2003 服务器系统的自动更新功能
422 0
如何关闭windows server2003 服务器系统的自动更新功能
|
C#
WinForm关闭主窗体后,仍然有后台进程运行。
WinForm关闭主窗体后,仍然有后台进程运行。
342 0
|
网络安全 数据安全/隐私保护 Windows