c# - 通过c#卸载windows服务后无法删除.exe文件
问题描述
我正在使用System.Configuration.Install.ManagedInstallerClass.InstallHelper
在另一个服务中安装和卸载 Windows 服务。问题是安装服务时,exe
服务被锁定或其他问题,但我需要在服务卸载后删除服务文件。
服务安装:
System.Configuration
.Install
.ManagedInstallerClass
.InstallHelper(new string[] { "/i", serviceExePath });
服务卸载:
System.Configuration
.Install
.ManagedInstallerClass
.InstallHelper(new string[] { "/u", serviceExePath });
解决方案
我遇到了同样的问题。事实证明,在ManageInstallerClass
服务上放置了一个文件句柄锁,并且在锁定过程终止之前您无法删除它。
我使用以下代码解决了这个问题:
var s = new ServiceInstaller
{
Context = new InstallContext(),
ServiceName = "<YOUR SERVICE NAME>"
};
s.Uninstall(null);
完成后,ServiceInstaller
您应该能够删除服务文件。
要使用,ServiceInstaller
您必须将其包含System.ServiceProcess
在您的课程中。
推荐阅读
- jenkins - 如何检查 Jenkins 构建是否正在等待输入?
- python - 将步长添加到线性优化
- python - 检查 NaN 时如何选择特定列 Pandas 数据框
- specflow - Specflow 场景大纲示例表 - 反对?
- javascript - 如何替换字符串中的一组字符
- r - 将多个日期格式因子转换为 R 中的日期类型
- iterm2 - iterm2 中的字体大小更改不会更改我的标签标题的字体大小。我怎样才能做到这一点?
- docker - 我可以在 Dockerfile 中引用 Dockerfile 吗?
- c# - ModelState 包含两次所需的字符串字符串
- c - 如何从 Byte [] 中造词?