进程编写很简单,可以创建,一个WINDOWS服务的项目就行,不过创建以后没有相关安装服务的配制,可以添加一个安装配制文件,
添加系统DLL引用,System.Configuration.Install
代码如下:
using System;
using System.Collections;
using System.ComponentModel;
using System.Configuration.Install;

namespace TransferService


{

/**//// <summary>
/// ProjectInstaller 的摘要说明。
/// </summary>
[RunInstaller(true)]
public class ProjectInstaller : System.Configuration.Install.Installer

{
private System.ServiceProcess.ServiceProcessInstaller serviceProcessInstaller1;
private System.ServiceProcess.ServiceInstaller serviceInstaller1;

/**//// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;

public ProjectInstaller()

{
// 该调用是设计器所必需的。
InitializeComponent();

// TODO: 在 InitializeComponent 调用后添加任何初始化
}


/**//// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )

{
if( disposing )

{
if(components != null)

{
components.Dispose();
}
}
base.Dispose( disposing );
}



组件设计器生成的代码#region 组件设计器生成的代码

/**//// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()

{
this.serviceProcessInstaller1 = new System.ServiceProcess.ServiceProcessInstaller();
this.serviceInstaller1 = new System.ServiceProcess.ServiceInstaller();
//
// serviceProcessInstaller1
//
this.serviceProcessInstaller1.Account = System.ServiceProcess.ServiceAccount.LocalSystem;
this.serviceProcessInstaller1.Password = null;
this.serviceProcessInstaller1.Username = null;
//
// serviceInstaller1
//
this.serviceInstaller1.DisplayName = "传输服务";
this.serviceInstaller1.ServiceName = "YJC.Transfer.Service";
this.serviceInstaller1.StartType = System.ServiceProcess.ServiceStartMode.Automatic;
//
// ProjectInstaller
//

this.Installers.AddRange(new System.Configuration.Install.Installer[]
{
this.serviceProcessInstaller1,
this.serviceInstaller1});

}
#endregion
}
}

主要服务运算代码可以在, Service1.cs的static void Main()或构造函数里写,
如果要编写程序控制服务,.Net Framework有一个, System.ServiceProcess.ServiceController的类,可以控制服务器,起动与暂停,停止等,默认没有暂停功能,如果需要,在服务代码里,需要,OVer相应的代码,默认Onstart与OnStop已经Override,
System.ServiceProcess.ServiceController控制起来很简单,设置属性:MachineName(服务器名)与ServiceName(进程名)就可以获得进程的状态了和控制了,
这个的进程名是对应Install.cs里的, this.serviceInstaller1.ServiceName = "YJC.Transfer.Service";这个属性
不过要次控制需要使用,Refresh();方法进行刷新,
相应我的代码如下:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.ServiceProcess;
using KitDescriptor;
namespace ServiceManager


{

/**//// <summary>
/// Form1 的摘要说明。
/// </summary>
[Kit("ServiceMange", DisplayName = "后台服务管理", PictureName = "WebCargoForm.gif", Author = "WangYJ",
Description = "服务管理", DisplayOrder = 1)]
public class AppMainForm : FormSolution.Library.UI.EditForm

{
private System.Windows.Forms.ComboBox cb_Service;

/**//// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
private FormSolution.Library.CustomControl.XPButton.XPButton btn_start;
private System.ServiceProcess.ServiceController fSericeControl;
private System.Windows.Forms.TextBox txt_MachineName;
private FormSolution.Library.CustomControl.XPButton.XPButton btn_Parse;
private FormSolution.Library.CustomControl.XPButton.XPButton btn_stop;
private FormSolution.Library.CustomControl.XPButton.XPButton btn_install;
private FormSolution.Library.CustomControl.XPButton.XPButton btn_uninstall;
private FormSolution.Library.CustomControl.XPButton.XPButton btn_refresh;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.GroupBox groupBox1;

private DataSet ds = null;
public AppMainForm()

{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
ds = new DataSet();
// cb_Service.DataSource = ServiceManager.GetObject.Tables[0];
string path = Application.StartupPath+"\\service.config";
ds.ReadXml(path,XmlReadMode.ReadSchema);
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
foreach(DataRow row in ds.Tables[0].Rows)

{
cb_Service.Items.Add(row["DisplayName"]);
}
}


/**//// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )

{
if( disposing )

{
if (components != null)

{
components.Dispose();
}
}
base.Dispose( disposing );
}


Windows 窗体设计器生成的代码#region Windows 窗体设计器生成的代码

/**//// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()

{
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(AppMainForm));
this.cb_Service = new System.Windows.Forms.ComboBox();
this.btn_start = new FormSolution.Library.CustomControl.XPButton.XPButton();
this.btn_Parse = new FormSolution.Library.CustomControl.XPButton.XPButton();
this.btn_stop = new FormSolution.Library.CustomControl.XPButton.XPButton();
this.btn_install = new FormSolution.Library.CustomControl.XPButton.XPButton();
this.fSericeControl = new System.ServiceProcess.ServiceController();
this.txt_MachineName = new System.Windows.Forms.TextBox();
this.btn_uninstall = new FormSolution.Library.CustomControl.XPButton.XPButton();
this.btn_refresh = new FormSolution.Library.CustomControl.XPButton.XPButton();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.SuspendLayout();
//
// bottomBg
//
this.bottomBg.Location = new System.Drawing.Point(0, 231);
this.bottomBg.Name = "bottomBg";
this.bottomBg.Size = new System.Drawing.Size(602, 32);
//
// Banner
//
this.Banner.Name = "Banner";
this.Banner.Size = new System.Drawing.Size(602, 64);
//
// cb_Service
//
this.cb_Service.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.cb_Service.Location = new System.Drawing.Point(104, 128);
this.cb_Service.Name = "cb_Service";
this.cb_Service.Size = new System.Drawing.Size(121, 20);
this.cb_Service.TabIndex = 0;
this.cb_Service.SelectedIndexChanged += new System.EventHandler(this.cb_Service_SelectedIndexChanged);
//
// btn_start
//
this.btn_start.AdjustImageLocation = new System.Drawing.Point(0, 0);
this.btn_start.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(0)), ((System.Byte)(255)), ((System.Byte)(255)), ((System.Byte)(255)));
this.btn_start.BtnStyle = FormSolution.Library.CustomControl.XPButton.XPStyle.Silver;
this.btn_start.Enabled = false;
this.btn_start.IcoType = FormSolution.Library.CustomControl.XPButton.IcoStyle.Edit;
this.btn_start.Image = ((System.Drawing.Image)(resources.GetObject("btn_start.Image")));
this.btn_start.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
this.btn_start.Location = new System.Drawing.Point(48, 168);
this.btn_start.Name = "btn_start";
this.btn_start.Size = new System.Drawing.Size(72, 28);
this.btn_start.TabIndex = 1;
this.btn_start.Text = "启动";
this.btn_start.Click += new System.EventHandler(this.btn_start_Click);
//
// btn_Parse
//
this.btn_Parse.AdjustImageLocation = new System.Drawing.Point(0, 0);
this.btn_Parse.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(0)), ((System.Byte)(255)), ((System.Byte)(255)), ((System.Byte)(255)));
this.btn_Parse.BtnStyle = FormSolution.Library.CustomControl.XPButton.XPStyle.Silver;
this.btn_Parse.Enabled = false;
this.btn_Parse.IcoType = FormSolution.Library.CustomControl.XPButton.IcoStyle.Affirm;
this.btn_Parse.Image = ((System.Drawing.Image)(resources.GetObject("btn_Parse.Image")));
this.btn_Parse.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
this.btn_Parse.Location = new System.Drawing.Point(152, 168);
this.btn_Parse.Name = "btn_Parse";
this.btn_Parse.Size = new System.Drawing.Size(72, 28);
this.btn_Parse.TabIndex = 2;
this.btn_Parse.Text = "暂停";
this.btn_Parse.Click += new System.EventHandler(this.btn_Parse_Click);
//
// btn_stop
//
this.btn_stop.AdjustImageLocation = new System.Drawing.Point(0, 0);
this.btn_stop.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(0)), ((System.Byte)(255)), ((System.Byte)(255)), ((System.Byte)(255)));
this.btn_stop.BtnStyle = FormSolution.Library.CustomControl.XPButton.XPStyle.Silver;
this.btn_stop.Enabled = false;
this.btn_stop.IcoType = FormSolution.Library.CustomControl.XPButton.IcoStyle.Login;
this.btn_stop.Image = ((System.Drawing.Image)(resources.GetObject("btn_stop.Image")));
this.btn_stop.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
this.btn_stop.Location = new System.Drawing.Point(248, 168);
this.btn_stop.Name = "btn_stop";
this.btn_stop.Size = new System.Drawing.Size(72, 28);
this.btn_stop.TabIndex = 3;
this.btn_stop.Text = "停止";
this.btn_stop.Click += new System.EventHandler(this.btn_stop_Click);
//
// btn_install
//
this.btn_install.AdjustImageLocation = new System.Drawing.Point(0, 0);
this.btn_install.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(0)), ((System.Byte)(255)), ((System.Byte)(255)), ((System.Byte)(255)));
this.btn_install.BtnStyle = FormSolution.Library.CustomControl.XPButton.XPStyle.Silver;
this.btn_install.Enabled = false;
this.btn_install.IcoType = FormSolution.Library.CustomControl.XPButton.IcoStyle.Preview;
this.btn_install.Image = ((System.Drawing.Image)(resources.GetObject("btn_install.Image")));
this.btn_install.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
this.btn_install.Location = new System.Drawing.Point(352, 168);
this.btn_install.Name = "btn_install";
this.btn_install.Size = new System.Drawing.Size(88, 28);
this.btn_install.TabIndex = 4;
this.btn_install.Text = "安装服务";
this.btn_install.Click += new System.EventHandler(this.btn_install_Click);
//
// txt_MachineName
//
this.txt_MachineName.Location = new System.Drawing.Point(104, 96);
this.txt_MachineName.Name = "txt_MachineName";
this.txt_MachineName.Size = new System.Drawing.Size(120, 21);
this.txt_MachineName.TabIndex = 5;
this.txt_MachineName.Text = ".";
//
// btn_uninstall
//
this.btn_uninstall.AdjustImageLocation = new System.Drawing.Point(0, 0);
this.btn_uninstall.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(0)), ((System.Byte)(255)), ((System.Byte)(255)), ((System.Byte)(255)));
this.btn_uninstall.BtnStyle = FormSolution.Library.CustomControl.XPButton.XPStyle.Silver;
this.btn_uninstall.Enabled = false;
this.btn_uninstall.IcoType = FormSolution.Library.CustomControl.XPButton.IcoStyle.New;
this.btn_uninstall.Image = ((System.Drawing.Image)(resources.GetObject("btn_uninstall.Image")));
this.btn_uninstall.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
this.btn_uninstall.Location = new System.Drawing.Point(456, 168);
this.btn_uninstall.Name = "btn_uninstall";
this.btn_uninstall.Size = new System.Drawing.Size(96, 28);
this.btn_uninstall.TabIndex = 6;
this.btn_uninstall.Text = "卸载服务";
this.btn_uninstall.Click += new System.EventHandler(this.btn_uninstall_Click);
//
// btn_refresh
//
this.btn_refresh.AdjustImageLocation = new System.Drawing.Point(0, 0);
this.btn_refresh.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(0)), ((System.Byte)(255)), ((System.Byte)(255)), ((System.Byte)(255)));
this.btn_refresh.BtnStyle = FormSolution.Library.CustomControl.XPButton.XPStyle.Silver;
this.btn_refresh.Enabled = false;
this.btn_refresh.IcoType = FormSolution.Library.CustomControl.XPButton.IcoStyle.Refresh;
this.btn_refresh.Image = ((System.Drawing.Image)(resources.GetObject("btn_refresh.Image")));
this.btn_refresh.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
this.btn_refresh.Location = new System.Drawing.Point(248, 128);
this.btn_refresh.Name = "btn_refresh";
this.btn_refresh.Size = new System.Drawing.Size(72, 28);
this.btn_refresh.TabIndex = 7;
this.btn_refresh.Text = "刷新";
this.btn_refresh.Click += new System.EventHandler(this.btn_refresh_Click);
//
// label1
//
this.label1.Location = new System.Drawing.Point(40, 96);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(56, 23);
this.label1.TabIndex = 8;
this.label1.Text = "服务器";
//
// label2
//
this.label2.Location = new System.Drawing.Point(40, 128);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(32, 23);
this.label2.TabIndex = 9;
this.label2.Text = "服务";
//
// groupBox1
//
this.groupBox1.Location = new System.Drawing.Point(32, 72);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(536, 144);
this.groupBox1.TabIndex = 22;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "服务管理";
//
// AppMainForm
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(602, 263);
this.Controls.Add(this.txt_MachineName);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.btn_refresh);
this.Controls.Add(this.btn_uninstall);
this.Controls.Add(this.btn_install);
this.Controls.Add(this.btn_stop);
this.Controls.Add(this.btn_Parse);
this.Controls.Add(this.btn_start);
this.Controls.Add(this.cb_Service);
this.Controls.Add(this.groupBox1);
this.Name = "AppMainForm";
this.Text = "服务进程管理";
this.Controls.SetChildIndex(this.groupBox1, 0);
this.Controls.SetChildIndex(this.cb_Service, 0);
this.Controls.SetChildIndex(this.btn_start, 0);
this.Controls.SetChildIndex(this.btn_Parse, 0);
this.Controls.SetChildIndex(this.btn_stop, 0);
this.Controls.SetChildIndex(this.btn_install, 0);
this.Controls.SetChildIndex(this.btn_uninstall, 0);
this.Controls.SetChildIndex(this.btn_refresh, 0);
this.Controls.SetChildIndex(this.label1, 0);
this.Controls.SetChildIndex(this.label2, 0);
this.Controls.SetChildIndex(this.txt_MachineName, 0);
this.Controls.SetChildIndex(this.bottomBg, 0);
this.Controls.SetChildIndex(this.Banner, 0);
this.ResumeLayout(false);

}
#endregion


/**//// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()

{
Application.Run(new AppMainForm());
}

private void cb_Service_SelectedIndexChanged(object sender, System.EventArgs e)

{
fSericeControl.MachineName = txt_MachineName.Text;
fSericeControl.ServiceName = getRow()["ServiceName"].ToString();
btn_refresh.Enabled = true;
CheckStatus();
}
public void CheckStatus()

{
System.Threading.Thread.Sleep(100);
btn_start.Enabled = false;
btn_stop.Enabled = false;
btn_Parse.Enabled = false;
btn_install.Enabled = false;
btn_uninstall.Enabled = false;
try

{
if(fSericeControl.Status == System.ServiceProcess.ServiceControllerStatus.Stopped)

{
btn_start.Enabled = true;
btn_uninstall.Enabled = true;
}
if(fSericeControl.CanPauseAndContinue)

{
btn_Parse.Enabled = true;
}
if(fSericeControl.CanStop)

{
btn_stop.Enabled = true;
}
}
catch

{
btn_install.Enabled = true;

}

}

private void btn_install_Click(object sender, System.EventArgs e)

{
try

{
CommandControls.ExcuteCmd(getRow()["InstallPath"].ToString());
System.Threading.Thread.Sleep(1000);
ShowMess("安装成功!");
}
catch(Exception ex)

{
ShowMess(ex.ToString());
}
CheckStatus();
}

private void btn_Parse_Click(object sender, System.EventArgs e)

{
if(fSericeControl.Status == System.ServiceProcess.ServiceControllerStatus.Running)

{
fSericeControl.Pause();
btn_Parse.Text = "继续";
}
else

{
fSericeControl.Continue();
btn_Parse.Text = "暂停";
}
CheckStatus();
}

private void btn_stop_Click(object sender, System.EventArgs e)

{
fSericeControl.Stop();
fSericeControl.Refresh();
CheckStatus();
}

private void btn_start_Click(object sender, System.EventArgs e)

{
fSericeControl.Start();
fSericeControl.Refresh();
CheckStatus();
}

private void btn_refresh_Click(object sender, System.EventArgs e)

{
fSericeControl.Refresh();
CheckStatus();
}

private void btn_uninstall_Click(object sender, System.EventArgs e)

{
try

{
CommandControls.ExcuteCmd(getRow()["UninstallPath"].ToString());
System.Threading.Thread.Sleep(1000);
ShowMess("卸载成功!");
}
catch(Exception ex)

{
ShowMess(ex.ToString());
}
CheckStatus();
}
public DataRow getRow()

{
if(cb_Service.Text != "")
return ds.Tables[0].Rows[cb_Service.SelectedIndex];
else
return null;
}
public void ShowMess(string msg)

{
MessageBox.Show(msg);
}

}
}

【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· [AI/GPT/综述] AI Agent的设计模式综述