In oob mode of silverlight, restart the process killself.

Source: Internet
Author: User

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();        }

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.