C # software random start-registry operation

Source: Internet
Author: User
Tags mssqlserver
C # software random start-registry operation

Recently, at the customer's requirement, a scheduled monitoring of Windows ServicesProgramTo enable the Service at random and regularly monitor the specified service. If the service is not enabled, it is enabled. Due to the rush of time, the server has not been running for a long time.CodeReleased, and will be updated in time for future improvements:
I. Ideas:
The core of this program is random start and Windows service. For random start, Microsoft. in the Win32 namespace, registrykey can be used to add, delete, and modify the Registry. For Windows Services, system is introduced. serviceprocess namespace. You can use the servicecontroller class to start, stop, and query system services. The program stability and resource consumption rate will be tested in another day.
II. The Code is as follows. Here, the program enables the MSSQLServer service by default and adds the tray area icon, which can be minimized to the tray area at startup or after startup:

[Copy to clipboard] Code:
Using system;
Using system. drawing;
Using system. collections;
Using system. componentmodel;
Using system. Windows. forms;
Using system. Data;
Using system. serviceprocess;
Using system. IO;
Using Microsoft. Win32;
Namespace watchservice
{
/// <Summary>
/// Summary of form1.
/// </Summary>
Public class watchservice: system. Windows. Forms. Form
{
Private system. Windows. Forms. Button btn_startwatch;
Private system. Windows. Forms. Button btn_stopwatch;
Private system. Windows. Forms. Button btn_starregulatory;
Private system. Windows. Forms. Button btn_stopreg;
Private system. Windows. Forms. Label lbl_appstatus;
Private system. Windows. Forms. textbox tbx_servicename;
Private system. Windows. Forms. Button btn_exit;
Private system. Windows. Forms. textbox tbx_interval;
Private system. Windows. Forms. Timer timer1;
Private system. Windows. Forms. policyicon policyicon1;
Private system. componentmodel. icontainer components;
Public watchservice ()
{
//
// Required for Windows Form Designer support
//
Initializecomponent ();
//
// Todo: add Any constructor code after initializecomponent calls
//
}
/// <Summary>
/// Clear all resources in use.
/// </Summary>
Protected override void dispose (bool disposing)
{
If (disposing)
{
If (components! = NULL)
{
Components. Dispose ();
}
}
Base. Dispose (disposing );
}
# Region code generated by Windows Form Designer
/// <Summary>
/// The designer supports the required methods-do not use the code editor to modify
/// Content of this method.
/// </Summary>
Private void initializecomponent ()
{
This. components = new system. componentmodel. Container ();
System. Resources. ResourceManager resources = new system. Resources. ResourceManager (typeof (watchservice ));
This. btn_startwatch = new system. Windows. Forms. Button ();
This. btn_stopwatch = new system. Windows. Forms. Button ();
This. btn_starregulatory = new system. Windows. Forms. Button ();
This. btn_stopreg = new system. Windows. Forms. Button ();
This. lbl_appstatus = new system. Windows. Forms. Label ();
This. tbx_servicename = new system. Windows. Forms. Textbox ();
This. btn_exit = new system. Windows. Forms. Button ();
This. tbx_interval = new system. Windows. Forms. Textbox ();
This. timer1 = new system. Windows. Forms. Timer (this. components );
This. policyicon1 = new system. Windows. Forms. policyicon (this. components );
This. suspendlayout ();
//
// Btn_startwatch
//
This. btn_startwatch.location = new system. Drawing. Point (112, 8 );
This. btn_startwatch.name = "btn_startwatch ";
This. btn_startwatch.size = new system. Drawing. Size (64, 23 );
This. btn_startwatch.tabindex = 0;
This. btn_startwatch.text = "start monitoring ";
This. btn_startwatch.click + = new system. eventhandler (this. btn_startwatch_click );
//
// Btn_stopwatch
//
This. btn_stopwatch.location = new system. Drawing. Point (184, 8 );
This. btn_stopwatch.name = "btn_stopwatch ";
This. btn_stopwatch.size = new system. Drawing. Size (64, 23 );
This. btn_stopwatch.tabindex = 1;
This. btn_stopwatch.text = "Stop monitoring ";
//
// Btn_starregulatory
//
This. btn_startreg.location = new system. Drawing. Point (112, 40 );
This. btn_startreg.name = "btn_starregulatory ";
This. btn_startreg.size = new system. Drawing. Size (88, 24 );
This. btn_startreg.tabindex = 2;
This. btn_startreg.text = "enable random start ";
This. btn_startreg.click + = new system. eventhandler (this. btn_startreg_click );
//
// Btn_stopreg
//
This. btn_stopreg.location = new system. Drawing. Point (232, 40 );
This. btn_stopreg.name = "btn_stopreg ";
This. btn_stopreg.size = new system. Drawing. Size (88, 24 );
This. btn_stopreg.tabindex = 3;
This. btn_stopreg.text = "Disable random start ";
This. btn_stopreg.click + = new system. eventhandler (this. btn_stopreg_click );
//
// Lbl_appstatus
//
This. lbl_appstatus.forecolor = system. Drawing. color. Red;
This. lbl_appstatus.location = new system. Drawing. Point (16, 72 );
This. lbl_appstatus.name = "lbl_appstatus ";
This. lbl_appstatus.size = new system. Drawing. Size (304, 23 );
This. lbl_appstatus.tabindex = 4;
This. lbl_appstatus.textalign = system. Drawing. contentalignment. middleleft;
//
// Tbx_servicename
//
This. tbx_servicename.borderstyle = system. Windows. Forms. borderstyle. fixedsingle;
This. tbx_servicename.forecolor = system. Drawing. color. Red;
This. tbx_servicename.location = new system. Drawing. Point (8, 8 );
This. tbx_servicename.name = "tbx_servicename ";
This. tbx_servicename.tabindex = 5;
This. tbx_servicename.text = "enter service name ";
This. tbx_servicename.mousedown + = new system. Windows. Forms. mouseeventhandler (this. tbx_servicename_mousedown );
//
// Btn_exit
//
This. btn_exit.location = new system. Drawing. Point (256, 8 );
This. btn_exit.name = "btn_exit ";
This. btn_exit.size = new system. Drawing. Size (64, 23 );
This. btn_exit.tabindex = 6;
This. btn_exit.text = "Exit program ";
This. btn_exit.click + = new system. eventhandler (this. btn_exit_click );
//
// Tbx_interval
//
This. tbx_interval.borderstyle = system. Windows. Forms. borderstyle. fixedsingle;
This. tbx_interval.forecolor = system. Drawing. color. Red;
This. tbx_interval.location = new system. Drawing. Point (8, 40 );
This. tbx_interval.name = "tbx_interval ";
This. tbx_interval.tabindex = 7;
This. tbx_interval.text = "input monitoring interval (seconds )";
This. tbx_interval.mousedown + = new system. Windows. Forms. mouseeventhandler (this. tbx_interval_mousedown );
//
// Timer1
//
This. timer1.tick + = new system. eventhandler (this. timerw.tick );
//
// Policyicon1
//
This. policyicon1.icon = (system. Drawing. Icon) (resources. GetObject ("policyicon1.icon ")));
This. policyicon1.text = "double-click to open watchservice ";
This. policyicon1.visible = true;
This. policyicon1.doubleclick + = new system. eventhandler (this. policyicon1_doubleclick );
//
// Watchservice
//
This. autoscalebasesize = new system. Drawing. Size (6, 14 );
This. clientsize = new system. Drawing. Size (328,102 );
This. Controls. Add (this. tbx_interval );
This. Controls. Add (this. btn_exit );
This. Controls. Add (this. tbx_servicename );
This. Controls. Add (this. lbl_appstatus );
This. Controls. Add (this. btn_stopreg );
This. Controls. Add (this. btn_starregulatory );
This. Controls. Add (this. btn_stopwatch );
This. Controls. Add (this. btn_startwatch );
This. maximizebox = false;
This. Name = "watchservice ";
This. showintaskbar = false;
This. Text = "watchservice ";
This. windowstate = system. Windows. Forms. formwindowstate. minimized;
This. Resize + = new system. eventhandler (this. watchservice_resize );
This. Load + = new system. eventhandler (this. watchservice_load );
This. resumelayout (false );
}
# Endregion
Private servicecontroller scdbservice;
Private string servicename = "MSSQLServer"; // default service name
Private int iinterval = 60; // default monitoring interval (seconds)
/// <Summary>
/// Main entry point of the application.
/// </Summary>
[Stathread]
Static void main ()
{
Application. Run (New watchservice ());
}
// Enable monitoring
Private void btn_startwatch_click (Object sender, system. eventargs E)
{
If (this. tbx_servicename.text = "")
{
This. lbl_appstatus.text = "service name cannot be blank ";
This. tbx_servicename.focus ();
Return;
}
If (this. tbx_interval.text = "")
{
This. lbl_appstatus.text = "the service monitoring interval cannot be blank ";
This. tbx_interval.focus ();
Return;
}
Servicename = This. tbx_servicename.text;
Iinterval = int. parse (this. tbx_interval.text );
This. timer1.integer = iinterval * 1000;
Startservice ();
}
// Enable random start
Private void btn_startreg_click (Object sender, system. eventargs E)
{
Try
{
String dir = directory. getcurrentdirectory ();
Dir + ="
\ Watchservice.exe ";
Registrykey akey = registry. localmachine;
Akey = akey. opensubkey (@ "SOFTWARE \ Microsoft \ Windows \ CurrentVersion \ Run", true );
Akey. setvalue ("watchservice", DIR );
Akey. Close ();
This. lbl_appstatus.text = "the random start is enabled successfully. ";
}
Catch (exception exp)
{
This. lbl_appstatus.text = "failed to enable random startup because:" + exp. message;
}
}
Private void tbx_servicename_mousedown (Object sender, system. Windows. Forms. mouseeventargs E)
{
This. tbx_servicename.text = "";
}

// Disable random start
Private void btn_stopreg_click (Object sender, system. eventargs E)
{
Try
{
Registrykey akey = registry. localmachine;
Akey = akey. opensubkey (@ "SOFTWARE \ Microsoft \ Windows \ CurrentVersion \ Run", true );
Akey. setvalue ("watchservice", false );
Akey. Close ();
This. lbl_appstatus.text = "the random start is disabled successfully. ";
}
Catch (exception exp)
{
This. lbl_appstatus.text = "failed to disable random startup because:" + exp. message;
}
}

Private void btn_exit_click (Object sender, system. eventargs E)
{
Application. Exit ();
}
/// <Summary>
/// Enable the specified Windows Service
/// </Summary>
Private void startservice ()
{
Try
{
Scdbservice = new servicecontroller (servicename );
Servicecontroller [] scallservice = servicecontroller. getservices ();
Int I = 0;
While (I <scallservice. length)
{
If (scallservice [I]. displayname = servicename)
{
If (scdbservice. Status. Equals (servicecontrollerstatus. Stopped ))
{
This. lbl_appstatus.text = servicename + "the service is starting ...... ";
Scdbservice. Start ();
}
Else if (scdbservice. Status. Equals (servicecontrollerstatus. Running ))
{
This. lbl_appstatus.text = servicename + "the service is running ...... ";
}
If (! This. timer1.enabled) This. timer1.start ();
Return;
}
Else
{
I ++;
}
}
This. lbl_appstatus.text = servicename + "service is not installed on the local machine. Please check. ";
If (this. timer1.enabled) This. timer1.stop ();
}
Catch (exception exp)
{
This. lbl_appstatus.text = servicename + "service startup failed, cause:" + exp. message;
}
}
// Monitor the clock
Private void timerjavastick (Object sender, system. eventargs E)
{
Startservice ();
}
Private void tbx_interval_mousedown (Object sender, system. Windows. Forms. mouseeventargs E)
{
This. tbx_interval.text = "";
}
// After the form is loaded, it is minimized to the tray area.
Private void watchservice_load (Object sender, system. eventargs E)
{
This. Hide ();
This. policyicon1.visible = true;
Startservice ();
}
// Minimize forms
Private void watchservice_resize (Object sender, system. eventargs E)
{
If (this. windowstate = formwindowstate. Minimized)
{
This. Hide ();
This. policyicon1.visible = true;
}
}
// Tray area icon operation
Private void policyicon#doubleclick (Object sender, system. eventargs E)
{
This. Visible = true;
This. windowstate = formwindowstate. normal;
This. policyicon1.visible = false;
}
// Stop monitoring
Private void btn_stopwatch_click (Object sender, system. eventargs E)
{
This. timer1.stop ();
}
}
}

Note: The simhei part is the random start part. After testing, you can directly copy the code and change it to your own name.

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.