1
需要是.Net Framework 项目
需要管理员权限才可以操作windows服务 所以添加清单文件
修改
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
即可实现默认管理员模式打开
2
安装Nuget包
正常其他会自动安装
private void Form1_Load(object sender, EventArgs e)
{
//是否强制重新安装服务
bool ReInstall = true;
string serviceName = "TlBlazorAmisApi";//服务名字
string servicePath = @"d:\aaa.exe";//路径 必须是绝对路径 并且路径中间不要包含空格
WindowsServiceShell ws = new WindowsServiceShell();
//查询服务
var any = ws.Get(serviceName);
//存在
if (any != null)
{
MessageBox.Show(any.AccountName + "\r\n" + any.DisplayName + "\r\n" + any.Description);
if (ReInstall)
{
MessageBox.Show("检测到强制重新安装!");
try
{
//卸载
ws.Uninstall(serviceName);
//安装
ws.Install(serviceName, servicePath);
}
catch (Exception ex)
{
MessageBox.Show("卸载失败!");
}
}
}
else
{
//不存在则安装
ws.Install(serviceName, servicePath);
}
}