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(); } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
2006-06-11 WINSTC(windows remote server/run time/remote client)自己想的b/s方案