卸载Windows服务的正确方法?
我有一个使用 C# 构建的 Windows 服务,通过 VS2008 安装项目安装,并且在卸载过程中遇到了一些问题:
服务未停止
卸载之前 卸载例程运行时,它会抛出有关文件正在使用的错误。单击“继续”即可正确完成安装程序,但该服务仍显示在列表中,因此未正确卸载。
(目前,我必须使用 sc delete servicename 手动删除它)。
我尝试使用以下代码在卸载之前停止服务,但它似乎没有生效:
protected override void OnBeforeUninstall(IDictionary savedState)
{
base.OnBeforeUninstall(savedState);
ServiceController serviceController = new ServiceController(MyInstaller.ServiceName);
serviceController.Stop();
}
何时调用此代码,以及如何在卸载之前停止服务?
卸载后安装文件夹未删除
该应用程序在执行时还会在其安装文件夹中创建一些文件。卸载后,安装文件夹 (C:\Program Files\MyApp) 并未被删除,其中包含应用程序创建的文件,但安装程序实际安装的所有其他文件均已成功删除。
卸载过程是否可以删除安装文件夹,包括该文件夹中生成的所有文件,如果可以,如何删除?
谢谢。
I've got a windows service, built using C#, that is installed via a VS2008 setup project, and am having a couple of problems occurring with the uninstall process:
Service is not stopped prior to uninstalling
When the uninstall routine runs, it throws up an error about files being in use. Clicking continue completes the installer correctly, but the service still shows up in the list, so it's not being uninstalled properly.
(At present, I have to resort to deleting it manually using sc delete servicename).
I'm trying to stop the service before uninstalling using the following code, but it doesn't seem to be taking effect:
protected override void OnBeforeUninstall(IDictionary savedState)
{
base.OnBeforeUninstall(savedState);
ServiceController serviceController = new ServiceController(MyInstaller.ServiceName);
serviceController.Stop();
}
When is this code called, and how can I stop the service prior to uninstalling?
Installation folder not deleted after uninstalling
The application also creates some files within it's installation folder when executed. After uninstalling, the installation folder (C:\Program Files\MyApp) is not deleted, and contains the files created by the application, though all other files that were actually installed by the installer have been deleted successfully.
Is it possible for the uninstall process to delete the installation folder, including all generated files within that folder, and if so, how?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为了任何寻求这些问题答案的人的利益:
卸载之前不会停止服务
尚未找到解决方案。
卸载后未删除安装文件夹
需要覆盖项目安装程序中的 OnAfterUninstall 方法,并且必须删除任何创建的文件。如果执行此步骤后应用程序安装程序文件夹不包含任何文件,则会自动删除该文件夹。
请记住,安装文件夹必须作为参数传入:
这会将安装文件夹作为参数传递给卸载例程,以便您知道应用程序的安装位置并可以删除生成的文件和文件夹。
For the benefit of anyone looking for an answer to these problems:
Service is not stopped prior to uninstalling
Haven't yet found a solution to this.
Installation folder not deleted after uninstalling
The OnAfterUninstall method in the project installer needs to be overridden, and any created files must be deleted. The application installer folder is automatically deleted if it doesn't contain any files after this step.
Remember, the installation folder must be passed in as a parameter:
This will pass the installation folder as a parameter to your uninstall routine so that you know where the application was installed and can delete your generated files and folders.
最有可能的是,服务只是需要一点时间来关闭,而您在服务完全停止之前继续运行。尝试调用 WaitForStatus(ServiceControllerStatus) 方法。
这将导致您的代码等待,直到服务处理“停止”消息并关闭。一旦服务实际关闭,它将不再保留任何文件句柄。
Most likely the service is just taking a little time to shut down, and you're continuing on before the service has fully stopped. Try calling the WaitForStatus(ServiceControllerStatus) method.
This will cause your code to wait until the service processes the "stop" message, and shuts down. Once the service is actually shut down, it will no longer be holding on to any file handles.
只要按照Mun的回答,通过在“OnAfterUninstall”函数中添加以下代码,Windows服务将被删除。
Just follow Mun's answer, by adding the following code in the "OnAfterUninstall" function, the Windows service will be deleted.