using System;
using System.Diagnostics;
using System.IO;
using System.ServiceProcess;
namespace PCMonitor.Installer {
///
/// Windows Service Installer
/// Menginstall/Uninstall Agent sebagai Windows Service
///
class ServiceInstaller {
private const string ServiceName = "PCMonitorAgent";
private const string ServiceDisplayName = "PC Monitor Agent";
private const string ServiceDescription = "Monitoring aktivitas PC - Agent Service";
// Entry point removed - use Program.cs Main instead
// Installation is called from INSTALL.bat or installer scripts
public static void InstallService() {
Console.WriteLine("Installing PC Monitor Agent Service...");
try {
// Check if service already exists
using (var sc = new ServiceController(ServiceName)) {
Console.WriteLine("Service already installed!");
return;
}
} catch (InvalidOperationException) {
// Service doesn't exist, proceed with installation
}
string exePath = System.Reflection.Assembly.GetExecutingAssembly().Location;
exePath = Path.Combine(Path.GetDirectoryName(exePath), "PCMonitorAgent.exe");
// Create service installation command using SC
string arguments = $"create \"{ServiceName}\" binPath= \"{exePath}\" " +
$"DisplayName= \"{ServiceDisplayName}\" start= auto";
ExecuteCommand("sc", arguments);
// Set service description
arguments = $"description \"{ServiceName}\" \"{ServiceDescription}\"";
ExecuteCommand("sc", arguments);
// Create directory for config and logs
string programDataPath = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData),
"PCMonitor"
);
Directory.CreateDirectory(programDataPath);
Directory.CreateDirectory(Path.Combine(programDataPath, "Logs"));
Directory.CreateDirectory(Path.Combine(programDataPath, "Screenshots"));
Console.WriteLine("Service installed successfully!");
Console.WriteLine($"Service Name: {ServiceName}");
Console.WriteLine($"Display Name: {ServiceDisplayName}");
Console.WriteLine("Run 'PCMonitorInstaller.exe start' to start the service");
}
public static void UninstallService() {
Console.WriteLine("Uninstalling PC Monitor Agent Service...");
try {
// Stop service first
using (var sc = new ServiceController(ServiceName)) {
if (sc.Status != ServiceControllerStatus.Stopped) {
Console.WriteLine("Stopping service...");
sc.Stop();
sc.WaitForStatus(ServiceControllerStatus.Stopped, TimeSpan.FromSeconds(30));
}
}
} catch (InvalidOperationException) {
// Service doesn't exist
}
// Delete service
string arguments = $"delete \"{ServiceName}\"";
ExecuteCommand("sc", arguments);
Console.WriteLine("Service uninstalled successfully!");
}
public static void StartService() {
Console.WriteLine("Starting PC Monitor Agent Service...");
try {
using (var sc = new ServiceController(ServiceName)) {
if (sc.Status == ServiceControllerStatus.Running) {
Console.WriteLine("Service is already running!");
return;
}
sc.Start();
sc.WaitForStatus(ServiceControllerStatus.Running, TimeSpan.FromSeconds(30));
Console.WriteLine("Service started successfully!");
}
} catch (Exception ex) {
Console.WriteLine($"Error starting service: {ex.Message}");
}
}
public static void StopService() {
Console.WriteLine("Stopping PC Monitor Agent Service...");
try {
using (var sc = new ServiceController(ServiceName)) {
if (sc.Status == ServiceControllerStatus.Stopped) {
Console.WriteLine("Service is already stopped!");
return;
}
sc.Stop();
sc.WaitForStatus(ServiceControllerStatus.Stopped, TimeSpan.FromSeconds(30));
Console.WriteLine("Service stopped successfully!");
}
} catch (Exception ex) {
Console.WriteLine($"Error stopping service: {ex.Message}");
}
}
public static void ShowStatus() {
try {
using (var sc = new ServiceController(ServiceName)) {
Console.WriteLine($"Service: {ServiceDisplayName}");
Console.WriteLine($"Status: {sc.Status}");
Console.WriteLine($"Can Handle Pause/Continue: {sc.CanPauseAndContinue}");
Console.WriteLine($"Service Type: {sc.ServiceType}");
}
} catch (InvalidOperationException) {
Console.WriteLine("Service is not installed!");
}
}
static void ExecuteCommand(string command, string arguments) {
var process = new Process {
StartInfo = new ProcessStartInfo {
FileName = command,
Arguments = arguments,
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
CreateNoWindow = true,
Verb = "runas"
}
};
try {
process.Start();
process.WaitForExit();
if (process.ExitCode != 0) {
string error = process.StandardError.ReadToEnd();
throw new Exception(error);
}
} finally {
process?.Dispose();
}
}
static void ShowUsage() {
Console.WriteLine("PC Monitor Agent Installer");
Console.WriteLine("Version 1.0");
Console.WriteLine();
Console.WriteLine("Usage: PCMonitorInstaller.exe ");
Console.WriteLine();
Console.WriteLine("Commands:");
Console.WriteLine(" install - Install service");
Console.WriteLine(" uninstall - Uninstall service");
Console.WriteLine(" start - Start service");
Console.WriteLine(" stop - Stop service");
Console.WriteLine(" status - Show service status");
Console.WriteLine();
Console.WriteLine("Note: Run with Administrator privileges");
}
}
}