Silverlight is currently developing applications that want to automatically restart the system after logging off the Sllauncher.exe, the implementation is through the WMI COM interface, get to the current application of the execution command line (CommandLine), and through the shell run; The code is as follows:
#regionUsing sectionusingSystem;usingSystem.Collections.Generic;usingSystem.Runtime.InteropServices.Automation;usingSystem.Windows;#endregionnamespacekillself{ Public classComobjecthelper {Private Staticlist<string>getprocess () {varresult =Newlist<string>(); Try{dynamic Objlocator= Automationfactory.createobject ("WbemScripting.SWbemLocator"); Dynamic objWMIService= Objlocator.connectserver (".","root\\cimv2"); Dynamic Query=objWMIService.ExecQuery ("Select * from Win32_Process"); foreach(Dynamic Oinchquery) { //String value = "ExecutablePath =" + O.commandline + "\ r \ n"; //Console.WriteLine (value);result. ADD (O.commandline+""); } returnresult; } Catch(Exception ex) {MessageBox.Show ("Get Process:"+Ex. Message); } return NULL; } Internal Static voidExec (stringcmdline) { Try{dynamic cmd= Automationfactory.createobject ("Wscript.Shell"); Cmd. Run (CmdLine,1,false); } Catch(Exception ex) {MessageBox.Show ("Execute Process:"+Ex. Message); } } Public Staticslprocess getself () {string[] procs =getprocess (). ToArray (); foreach(stringPinchprocs) { stringCmdLine =P.tolower (); if(CmdLine. IndexOf ("Sllauncher.exe", stringcomparison.ordinal)! =-1) { return Newslprocess (cmdline); } } Throw NewNullReferenceException ("SLLauncher.exe process not found"); } } Public classslprocess { PublicSlprocess (stringcmdline) {CommandLine=CmdLine; } Public stringCommandLine {Get;Set; } Publicslprocess Run () {comobjecthelper.exec (CommandLine); return This; } Publicslprocess Kill () {Application.Current.MainWindow.Close (); return This; } }}
Invocation part:
?
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(); }
|