Currently, the application developed by silverlight wants to automatically restart sllauncher.exe after logging out of the system. The implementation method is to obtain the execution command line (CommandLine) of the current application through the COM interface of WMI ); the Code is as follows:
# Region Using Sectionusing System; using System. collections. generic; using System. runtime. interopServices. automation; using System. windows; # endregionnamespace KillSelf {public class ComObjectHelper {private static List <string> GetProcess () {var result = new List <string> (); try {dynamic objLocator = AutomationFactory. createObject ("WbemScripting. SWbemLocator "); dynamic ob1_miservice = objLocator. conn EctServer (". "," root \ cimv2 "); dynamic query = ob1_miservice. 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 cmdli Ne) {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 using line = p. toLower (); if (rows line. indexOf ("sllauncher.exe", StringComparison. ordinal )! =-1) {return new SlProcess (cmdline);} throw new NullReferenceException ("sllauncher.exe process not found");} 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 ;}}}
Call part:
private void Button_Click(object sender, RoutedEventArgs e) { if (!Application.Current.IsRunningOutOfBrowser) { MessageBox.Show("Not Running OutOfBrowser!!"); return; } ComObjectHelper.GetSelf().Run().Kill(); }