使用 AssemblyInstaller 卸载服务后,我无法删除 .exe 文件
After uninstalling service using AssemblyInstaller I can not delete .exe file
我在我的代码中使用 AssemblyInstaller
来删除服务,然后我尝试删除 .exe 文件。
但是发生错误说:
The file is used by another process.
我试过了assemblyInstaller.dispose()
但它不起作用。
using (AssemblyInstaller installer = new AssemblyInstaller(filepath, commandLineOptions))
{
installer.UseNewContext = true;
installer.Uninstall(null);
installer.Dispose();
}
使用Resource Monitor
查找进程打开的文件句柄。一旦找到该进程,您可以终止它以释放文件句柄,从而允许您删除它。
在别人的帮助下,我发现使用 AssemblyInstaller 导致了问题。要卸载服务,我们可以使用其他方法,如 InstallUtil.exe 或 ServiceInstaller。我们可以使用ServiceInstaller代替AssemblyInstaller来卸载,不会出现这个问题。
ServiceInstaller ServiceInstallerObj = new ServiceInstaller();
InstallContext Context = new InstallContext(AppDomain.CurrentDomain.BaseDirectory + "\uninstalllog.log", null);
ServiceInstallerObj.Context = Context;
ServiceInstallerObj.ServiceName = "myService";
ServiceInstallerObj.Uninstall(null);
这将删除指定名称的服务,并且可以毫无问题地删除 .exe 文件
编辑
我发现使用 AppDomain
我们可以卸载程序集
var domain = AppDomain.CreateDomain("MyDomain");
using (AssemblyInstaller installer = domain.CreateInstance(typeof(AssemblyInstaller).Assembly.FullName, typeof(AssemblyInstaller).FullName, false, BindingFlags.Public | BindingFlags.CreateInstance | BindingFlags.Instance | BindingFlags.ExactBinding, null, new Object[] { servicePath, new String[] { } }, null, null, null).Unwrap() as AssemblyInstaller)
{
installer.UseNewContext = true;
installer.Uninstall(null);
}
AppDomain.Unload(domain);
我在我的代码中使用 AssemblyInstaller
来删除服务,然后我尝试删除 .exe 文件。
但是发生错误说:
The file is used by another process.
我试过了assemblyInstaller.dispose()
但它不起作用。
using (AssemblyInstaller installer = new AssemblyInstaller(filepath, commandLineOptions))
{
installer.UseNewContext = true;
installer.Uninstall(null);
installer.Dispose();
}
使用Resource Monitor
查找进程打开的文件句柄。一旦找到该进程,您可以终止它以释放文件句柄,从而允许您删除它。
在别人的帮助下,我发现使用 AssemblyInstaller 导致了问题。要卸载服务,我们可以使用其他方法,如 InstallUtil.exe 或 ServiceInstaller。我们可以使用ServiceInstaller代替AssemblyInstaller来卸载,不会出现这个问题。
ServiceInstaller ServiceInstallerObj = new ServiceInstaller();
InstallContext Context = new InstallContext(AppDomain.CurrentDomain.BaseDirectory + "\uninstalllog.log", null);
ServiceInstallerObj.Context = Context;
ServiceInstallerObj.ServiceName = "myService";
ServiceInstallerObj.Uninstall(null);
这将删除指定名称的服务,并且可以毫无问题地删除 .exe 文件
编辑
我发现使用 AppDomain
我们可以卸载程序集
var domain = AppDomain.CreateDomain("MyDomain");
using (AssemblyInstaller installer = domain.CreateInstance(typeof(AssemblyInstaller).Assembly.FullName, typeof(AssemblyInstaller).FullName, false, BindingFlags.Public | BindingFlags.CreateInstance | BindingFlags.Instance | BindingFlags.ExactBinding, null, new Object[] { servicePath, new String[] { } }, null, null, null).Unwrap() as AssemblyInstaller)
{
installer.UseNewContext = true;
installer.Uninstall(null);
}
AppDomain.Unload(domain);