C #, using the DOS command to close the currently running programs and restart

         In C # can be used to perform many operations invoked Dos command Pocess.Start, and very convenient. For example closes the current application, restart the computer and so on.

        Here are a few commonly used commands:

// restart the computer command

private void RestartPC()

{

        // restart the computer commands issued
        Process.Start ( "the Shutdown.exe", "-r");
        // close all related processes
        Process.GetCurrentProcess () Kill ().;

}

// Turn off the computer command

private void ShutDownPC()

{

        // restart the computer commands issued
        Process.Start ( "the Shutdown.exe");
        // close all related processes
        Process.GetCurrentProcess () Kill ().;

}


// Turn off the computer overloaded function, you can set the countdown

        public static void ShutDownPC(string time)
        {
            string s = "-s -t " + time;
            Process.Start("shutdown.exe", s);

        }

        //logout

        public static void LogOff()
        {
            Process.Start("shutdown.exe", "-l");
        }

         // Turn off the computer revoked

         public static void CancelShutDown()
        {
            Process.Start("shutdown.exe", "-a");
        }


// open an application, such as turning upgrade program

    /// <Summary>
    /// upgrade program start
    /// </ Summary>
    Private void StartUpdateSys ()
        {
        // Start upgrade
        Process [] = VAproc Process.GetProcessesByName ( "VersionAgent");
        IF (= VAproc.Length 0 =)
            {
            Process proc = new new Process ();
            proc.StartInfo.FileName = "VersionAgent.exe";
            proc.StartInfo.Arguments = "/ P1 / b38400 / fstock / of Mr";
            proc.StartInfo.WindowStyle = the System.Diagnostics .ProcessWindowStyle.Hidden;
            proc.Start ();
            }
        }


            /// <Summary> 
            /// boot operation setup 
            /// </ Summary> 
            /// <param name = "Started"> whether power running </ param> 
            /// <param name = "exeName"> to EXE program name to run (do not expand the name) </ param> 
            /// <param name = "path"> EXE to run the program path </ param> 
            /// <returns a> successful return true, otherwise returns false </ returns A> 

            public BOOL RunWhenStart (BOOL Started, exeName String, String path) 
            { 
                RegistryKey Key = Registry.LocalMachine.OpenSubKey ( "SOFTWARE \\ in the Microsoft \\ Windows \\ CurrentVersion \\ the Run", to true); // open the registry child item 
                if (key == null) // If the key does not exist, create the subkey  
                {
                    key = Registry.LocalMachine.CreateSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run");
                } 
                IF (== Started to true) 
                { 
                    the try 
                    { 
                        key.SetValue (exeName, path); // set to boot 
                        key.Close (); 
                    } 
                    the catch 
                    { 
                        return to false; 
                    } 
                } 
                the else 
                {
                    try
                    {
                        key.DeleteValue (exeName); // cancel boot 
                        key.Close (); 
                    } 
                    the catch 
                    { 
                        return to false; 
                    } 
                } 
                return to true; 
            }







Reproduced in: https: //www.cnblogs.com/kevinGao/archive/2011/11/01/2231519.html

Guess you like

Origin blog.csdn.net/weixin_34186950/article/details/93054171