PDA

View Full Version : سوال: نصب WIndows Service با button



User_Soual
پنج شنبه 06 دی 1397, 00:00 صبح
با سلام به اساتید گرامی بنده یک پروژ سی شارپ داشتم می خواستم وقتی روی یک Button در روی فرم کلیک می شود بیاد windows service که نوشتم را نصب کنم طریقه کار را توضیح میدهید
اگر هم منظورم واضح نبود بگید
باتشکر

Mahmoud.Afrad
جمعه 07 دی 1397, 23:07 عصر
کلاس زیر را به پروژه ویندوزفرم اضافه کنید
using System;
using System.Collections;
using System.Configuration.Install;
using System.Linq;
using System.ServiceProcess;

public class WindowsServiceAssemblyInstaller
{
private string _serviceName;
private Type _serviceType;

public WindowsServiceAssemblyInstaller(string serviceName , Type serviceType)
{
_serviceName = serviceName;
_serviceType = serviceType;
}



private bool IsInstalled()
{
return ServiceController.GetServices().Any(item => item.ServiceName == _serviceName);
}

private bool IsRunning()
{
if (!IsInstalled()) return false;
using (ServiceController controller =new ServiceController(_serviceName))
{
return (controller.Status == ServiceControllerStatus.Running);
}
}

private AssemblyInstaller GetInstaller()
{
AssemblyInstaller installer = new AssemblyInstaller(_serviceType.Assembly, null);
installer.UseNewContext = true;
return installer;
}

public void InstallService()
{
if (IsInstalled()) return;

try
{
using (AssemblyInstaller installer = GetInstaller())
{
IDictionary state = new Hashtable();
try
{
installer.Install(state);
installer.Commit(state);
}
catch
{
try
{
installer.Rollback(state);
}
catch
{
}

throw;
}
}
}
catch
{
throw;
}
}

public void UninstallService()
{
if (!IsInstalled()) return;
try
{
using (AssemblyInstaller installer = GetInstaller())
{
IDictionary state = new Hashtable();
try
{
installer.Uninstall(state);
}
catch
{
throw;
}
}
}
catch
{
throw;
}
}

public void StartService()
{
if (!IsInstalled()) return;

using (ServiceController controller =
new ServiceController(_serviceName))
{
try
{
if (controller.Status != ServiceControllerStatus.Running)
{
controller.Start();
controller.WaitForStatus(ServiceControllerStatus.R unning,
TimeSpan.FromSeconds(10));
}
}
catch
{
throw;
}
}
}

public void StopService()
{
if (!IsInstalled()) return;
using (ServiceController controller =
new ServiceController(_serviceName))
{
try
{
if (controller.Status != ServiceControllerStatus.Stopped)
{
controller.Stop();
controller.WaitForStatus(ServiceControllerStatus.S topped,
TimeSpan.FromSeconds(10));
}
}
catch
{
throw;
}
}
}
}
به صورت زیر استفاده کنید(فرض که در فرم1 دو دکمه دارید)
private WindowsServiceAssemblyInstaller wsaInstaller;
public Form1()
{
InitializeComponent();

wsaInstaller = new WindowsServiceAssemblyInstaller("Service1" , typeof(WindowsService1.Service1));
}

private void btnInstall_Click(object sender, EventArgs e)
{
try
{
wsaInstaller.InstallService();
}
catch (Exception exception)
{
MessageBox.Show(exception.Message);
}
}

private void btnUninstall_Click(object sender, EventArgs e)
{
try
{
wsaInstaller.UninstallService();
}
catch (Exception exception)
{
MessageBox.Show(exception.Message);
}
}
پروژه ویندوزسرویس را به رفرنسهای پروژه بایست اضافه کرده باشید.(WindowsService1 فضای نام و Service1 کلاس ویندوزسرویس است.)

منبع
https://blogs.msdn.microsoft.com/helloworld/2008/10/20/how-to-install-windows-service-programmatically/
https://stackoverflow.com/a/1195621

User_Soual
شنبه 08 دی 1397, 00:22 صبح
ممنون این برنامه یعنی وقتی button (نصب ) رو زدم سرویس نصب میشه
لطفا یک توضیحاتی هم ارائه کنید