1
需要是.Net Framework 項(xiàng)目
需要管理員權(quán)限才可以操作windows服務(wù) 所以添加清單文件
修改
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
即可實(shí)現(xiàn)默認(rèn)管理員模式打開(kāi)
2
安裝Nuget包
正常其他會(huì)自動(dòng)安裝
private void Form1_Load(object sender, EventArgs e)
{
//是否強(qiáng)制重新安裝服務(wù)
bool ReInstall = true;
string serviceName = "TlBlazorAmisApi";//服務(wù)名字
string servicePath = @"d:\aaa.exe";//路徑 必須是絕對(duì)路徑 并且路徑中間不要包含空格
WindowsServiceShell ws = new WindowsServiceShell();
//查詢服務(wù)
var any = ws.Get(serviceName);
//存在
if (any != null)
{
MessageBox.Show(any.AccountName + "\r\n" + any.DisplayName + "\r\n" + any.Description);
if (ReInstall)
{
MessageBox.Show("檢測(cè)到強(qiáng)制重新安裝!");
try
{
//卸載
ws.Uninstall(serviceName);
//安裝
ws.Install(serviceName, servicePath);
}
catch (Exception ex)
{
MessageBox.Show("卸載失敗!");
}
}
}
else
{
//不存在則安裝
ws.Install(serviceName, servicePath);
}
}