说明
项目中的一个需求是定期将服务器A中的文件夹及所有文件转存到B服务器中。
解决思路:在A服务器中注册一个Windows服务,通过Timer来控制定期执行,读取A服务器本地文件夹及文件通过FTP的方式上传到B服务器对应的文件夹中。
以下代码是做了一个服务注册工具
效果如下图:
实现代码如下:
using System;
using System.Windows.Forms;
using System.ServiceProcess;
using System.Configuration.Install;
using System.Collections;
using System.ComponentModel;
using System.Runtime.CompilerServices;
namespace SyncAPP
{
public partial class FrmMain : Form
{
public FrmMain()
{
InitializeComponent();
}
string serviceFilePath = $"{Application.StartupPath}\\SyncService.exe";
string serviceName = "SyncMainService";
private void FrmMain_Load(object sender, EventArgs e)
{
}
/// <summary>
/// 判断服务是否存在
/// </summary>
/// <param name="serviceName">服务名称</param>
/// <returns></returns>
private bool IsServiceExisted(string serviceName)
{
ServiceController[] services = ServiceController.GetServices();
foreach (ServiceController sc in services)
{
if (sc.ServiceName.ToLower() == serviceName.ToLower())
{
return true;
}
}
return false;
}
/// <summary>
/// 安装服务
/// </summary>
/// <param name="serviceFilePath"></param>
private void InstallService(string serviceFilePath)
{
using (AssemblyInstaller installer = new AssemblyInstaller())
{
installer.UseNewContext = true;
installer.Path = serviceFilePath;
IDictionary savedState = new Hashtable();
installer.Install(savedState);
installer.Commit(savedState);
MessageBox.Show("安装成功!", "安装服务", MessageBoxButtons.OK);
}
}
/// <summary>
/// 卸载服务
/// </summary>
/// <param name="serviceFilePath"></param>
private void UninstallService(string serviceFilePath)
{
using (AssemblyInstaller installer = new AssemblyInstaller())
{
installer.UseNewContext = true;
installer.Path = serviceFilePath;
installer.Uninstall(null);
MessageBox.Show("卸载成功!", "卸载服务", MessageBoxButtons.OK);
}
}
/// <summary>
/// 启动服务
/// </summary>
/// <param name="serviceName"></param>
private void ServiceStart(string serviceName)
{
using (ServiceController control = new ServiceController(serviceName))
{
if (control.Status == ServiceControllerStatus.Stopped)
{
control.Start();
MessageBox.Show("启动成功!", "启动服务", MessageBoxButtons.OK);
}
}
}
/// <summary>
/// 停止服务
/// </summary>
/// <param name="serviceName"></param>
private void ServiceStop(string serviceName)
{
using (ServiceController control = new ServiceController(serviceName))
{
if (control.Status == ServiceControllerStatus.Running)
{
control.Stop();
MessageBox.Show("停止成功!", "停止服务", MessageBoxButtons.OK);
}
}
}
private void FrmMain_FormClosing(object sender, FormClosingEventArgs e)
{
//if (this.IsServiceExisted(serviceName)) this.UninstallService(serviceFilePath);
}
private void btnSyncStart_Click(object sender, EventArgs e)
{
if (this.IsServiceExisted(serviceName)) this.ServiceStart(serviceName);
}
private void btnSyncClose_Click(object sender, EventArgs e)
{
if (this.IsServiceExisted(serviceName)) this.ServiceStop(serviceName);
}
private void btn_init_Click(object sender, EventArgs e)
{
if (this.IsServiceExisted(serviceName)) this.UninstallService(serviceFilePath);
this.InstallService(serviceFilePath);
}
private void btn_uninstall_Click(object sender, EventArgs e)
{
if (this.IsServiceExisted(serviceName)) this.UninstallService(serviceFilePath);
}
}
}
Windows服务中做逻辑代码:
using Library.Interface;