|
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Reflection;
using System.Windows.Forms;
using System.ServiceProcess;
using ReputationLib;
// This application allows you to manage the reputationator swindows service without
// having to use the windows commandline.
namespace RepSvcManager
{
public partial class FormManageSvc : Form
{
bool m_installed = false;
ServiceController m_service = null;
bool m_installing = false;
private string m_svcPath = "";
private string m_installUtilPath = "";
private BackgroundWorker m_worker = null;
private delegate void DelegateUpdateForm();
//--------------------------------------------------------------------------------
/// <summary>
/// Constructor
/// </summary>
public FormManageSvc()
{
InitializeComponent();
}
//--------------------------------------------------------------------------------
/// <summary>
/// Retrieves and updates the current status of the service, and enables/disables
/// the buttons to reflect the possible actions the user can use.
/// </summary>
private void ShowServiceStatus()
{
ServiceController service = GetService();
if (service != null)
{
this.labelStatus.Text = service.Status.ToString();
m_installed = true;
}
else
{
this.labelStatus.Text = "NOT INSTALLED";
m_installed = false;
}
m_service = service;
buttonInstallUninstall.Text = (m_installed) ? "Uninstall" : "Install";
if (m_service != null)
{
buttonStartStop.Text = (m_installed && service.Status == ServiceControllerStatus.Stopped) ? "Start" : "Stop";
buttonPauseResume.Text = (m_installed && service.Status == ServiceControllerStatus.Paused) ? "Resume" : "Pause";
}
buttonPauseResume.Enabled = (service != null && service.CanPauseAndContinue);
buttonStartStop.Enabled = (m_installed);
}
//--------------------------------------------------------------------------------
/// <summary>
/// Locates the path of the service executable (remember, it is assumed that ALL
/// of the components of this software are found n the same folder).
/// </summary>
private void FindPaths()
{
// this is the executable for our service
m_svcPath = Assembly.GetExecutingAssembly().Location;
m_svcPath = System.IO.Path.GetDirectoryName(m_svcPath);
m_svcPath = System.IO.Path.Combine(m_svcPath, "ReputationScraperSvc.exe");
// find the system path, and then the newest copy of installutil.exe
string rootpath = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(Environment.SystemDirectory), @"Microsoft.Net\Framework");
DirectoryInfo di = new DirectoryInfo(rootpath);
FileInfo[] files = di.GetFiles("installutil.exe", SearchOption.AllDirectories);
foreach(FileInfo fi in files)
{
if (fi.FullName.Contains(@"Framework\v4"))
{
m_installUtilPath = fi.DirectoryName;
break;
}
}
}
//--------------------------------------------------------------------------------
/// <summary>
/// Retrieves the service if possible.
/// </summary>
/// <returns></returns>
private ServiceController GetService()
{
ServiceController[] controllers = ServiceController.GetServices();
var service = (from svc in controllers
where svc.ServiceName == "Reputationator_Service"
select svc).FirstOrDefault();
return service;
}
//--------------------------------------------------------------------------------
/// <summary>
/// Fires when the form is loaded.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void FormManageSvc_Load(object sender, EventArgs e)
{
FindPaths();
// start the background worker
m_worker = new BackgroundWorker();
m_worker.WorkerReportsProgress = true;
m_worker.WorkerSupportsCancellation = true;
m_worker.ProgressChanged += new ProgressChangedEventHandler(m_worker_ProgressChanged);
m_worker.DoWork += new DoWorkEventHandler(m_worker_DoWork);
m_worker.RunWorkerAsync();
}
//--------------------------------------------------------------------------------
/// <summary>
/// Fired when the background worker is started
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void m_worker_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker worker = sender as BackgroundWorker;
int interval = 1000;
// make a nuisance of ourselves
while (!worker.CancellationPending)
{
worker.ReportProgress(0);
Thread.Sleep(interval);
}
}
//--------------------------------------------------------------------------------
/// <summary>
/// Fired by the background worker when a progress event is sent, and invokes
/// the UI update method.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void m_worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
// tell the UI to update itself
DelegateUpdateForm method = new DelegateUpdateForm(ShowServiceStatus);
Invoke(method);
}
//--------------------------------------------------------------------------------
/// <summary>
/// Fired when the user clicks the Start/Stop button, and starts/stops the
/// service if possible.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonStartStop_Click(object sender, EventArgs e)
{
Button button = sender as Button;
ServiceController service = GetService();
switch (service.Status)
{
case ServiceControllerStatus.Running :
if (service.CanStop)
{
service.Stop();
button.Text = "Start";
}
break;
case ServiceControllerStatus.Stopped :
service.Start();
button.Text = "Stop";
break;
}
}
//--------------------------------------------------------------------------------
/// <summary>
/// Fired when the user clicks the Pause/Resume button, and pauses or resumes the
/// windows service if possible.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonPauseResume_Click(object sender, EventArgs e)
{
Button button = sender as Button;
ServiceController service = GetService();
switch (service.Status)
{
case ServiceControllerStatus.Running :
service.Pause();
button.Text = "Resume";
break;
case ServiceControllerStatus.Paused :
service.Continue();
button.Text = "Pause";
break;
}
}
//--------------------------------------------------------------------------------
/// <summary>
/// Fired when the user clicks the Install/Uninstall button, and will attempt to
/// install or undinstall the service whichever is indicated by the button text.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonInstallUninstall_Click(object sender, EventArgs e)
{
// determine our install/uninstall parameter
string param = (m_installed) ? "/u" : "/i";
m_installing = (param == "/i");
// build our parameter string
string args = string.Format("{0} {1}", (m_installed)?"/u":"/i", m_svcPath);
// start in this folder
ExternalProcess.WorkingDir = m_installUtilPath;
ExternalProcess.SetWorkingDir = true;
ExternalProcess.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
// run installutil
ExternalProcess.Run("installutil.exe", args, true);
}
//--------------------------------------------------------------------------------
/// <summary>
/// Fired when the OK button is clicked.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonOK_Click(object sender, EventArgs e)
{
m_worker.CancelAsync();
Close();
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
I've been paid as a programmer since 1982 with experience in Pascal, and C++ (both self-taught), and began writing Windows programs in 1991 using Visual C++ and MFC. In the 2nd half of 2007, I started writing C# Windows Forms and ASP.Net applications, and have since done WPF, Silverlight, WCF, web services, and Windows services.
My weakest point is that my moments of clarity are too brief to hold a meaningful conversation that requires more than 30 seconds to complete. Thankfully, grunts of agreement are all that is required to conduct most discussions without committing to any particular belief system.