使用AssemblyInstaller卸载服务后,卸载程序不关闭,就无法删除服务对应的exe的问题

2 篇文章 0 订阅

原始代码如下:

public static bool  UnintService(String serverName, string exeName)
{
    try
    {
	//UnInstall Service  
	if (CheckService(serverName))
	{
		var myAssemblyInstaller = new AssemblyInstaller
		{
			UseNewContext = true,
			Path = System.Environment.CurrentDirectory + "\\" + exeName
		};
		myAssemblyInstaller.Uninstall(null);		
		myAssemblyInstaller.Dispose();
		myAssemblyInstaller = null;
		return true;
	}
	else
	{
		return true;
	}

	}
	catch (Exception ex)
	{
		throw new Exception("unInstallServiceError/n" + ex.Message);
		return false;
	}
}

发现我再执行完这个卸载步骤后,想通过本身这个程序,删掉服务对应的exe文件,就会报该文件被占用,无法删除。

被这个问题困扰了很长时间,具体原因不明,目前通过一个new

AppDomain删除后,再关闭该AppDomain解决。代码如下:

public static bool  UnintService(String serverName, string exeName)
{
	try
	{
		//UnInstall Service  
		if (CheckService(serverName))
		{
			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[] { System.Environment.CurrentDirectory + "\\" + exeName, new String[] { } }, null, null, null).Unwrap() as AssemblyInstaller)
			{
				installer.UseNewContext = true;
				installer.Uninstall(null);
				installer.Dispose();
			}
			AppDomain.Unload(domain);
			return true;
		}
		else
		{
			return true;
		}

	}
	catch (Exception ex)
	{
		throw new Exception("unInstallServiceError/n" + ex.Message);
	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值