简体   繁体   English

正确的卸载Windows服务的方法?

[英]Correct way to uninstall a Windows service?

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: 我有一个使用C#构建的Windows服务,它是通过VS2008安装项目安装的,并且在卸载过程中遇到了一些问题:

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 ). (目前,我不得不使用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. 卸载后,安装文件夹(C:\\ Program Files \\ MyApp)不会被删除,并且包含应用程序创建的文件,但安装程序实际安装的所有其他文件都已成功删除。

Is it possible for the uninstall process to delete the installation folder, including all generated files within that folder, and if so, how? 卸载过程是否可以删除安装文件夹,包括该文件夹中的所有生成文件,如果是,如何删除?

Thanks. 谢谢。

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. 需要重写项目安装程序中的OnAfterUninstall方法,并且必须删除任何创建的文件。 The application installer folder is automatically deleted if it doesn't contain any files after this step. 如果在此步骤后不包含任何文件,则会自动删除应用程序安装程序文件夹。

protected override void OnAfterUninstall(IDictionary savedState)
{
    base.OnAfterUninstall(savedState);

    string targetDir = Context.Parameters["TargetDir"]; // Must be passed in as a parameter

    if (targetDir.EndsWith("|"))
        targetDir = targetDir.Substring(0, targetDir.Length-1);

    if (!targetDir.EndsWith("\\"))
        targetDir += "\\";

    if (!Directory.Exists(targetDir))
    {
        Debug.WriteLine("Target dir does not exist: " + targetDir);
        return;
    }

    string[] files = new[] { "File1.txt", "File2.tmp", "File3.doc" };
    string[] dirs  = new[] { "Logs", "Temp" };

    foreach (string f in files)
    {
        string path = Path.Combine(targetDir, f);

        if (File.Exists(path))
            File.Delete(path);
    }

    foreach (string d in dirs)
    {
        string path = Path.Combine(targetDir, d);

        if (Directory.Exists(d))
            Directory.Delete(d, true);
    }

    // At this point, all generated files and directories must be deleted.
    // The installation folder will be removed automatically.
}

Remember, the installation folder must be passed in as a parameter: 请记住,安装文件夹必须作为参数传入:

  • Right click on your setup project, then choose View -> Custom Actions 右键单击您的安装项目,然后选择View - > Custom Actions
  • The custom actions will open in your main window. 自定义操作将在主窗口中打开。 Right click on "Primary output from XXX" under the Uninstall node and select "Properties Window" 右键单击Uninstall节点下的“XXX的主输出”,然后选择“属性窗口”
  • In the properties window, under CustomActionData, enter the following: /TargetDir="[TARGETDIR]|" 在属性窗口的CustomActionData下,输入以下内容: / TargetDir =“[TARGETDIR] |” (note the pipe at the end, do not remove this). (注意最后的管道,不要删除它)。

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. 这会将安装文件夹作为参数传递给卸载例程,以便您知道应用程序的安装位置,并可以删除生成的文件和文件夹。

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. 尝试调用WaitForStatus(ServiceControllerStatus)方法。

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. 一旦服务实际关闭,它将不再保留任何文件句柄。

Just follow Mun's answer, by adding the following code in the "OnAfterUninstall" function, the Windows service will be deleted. 只需按照Mun的回答,通过在“OnAfterUninstall”功能中添加以下代码,Windows服务将被删除。

        var process = new System.Diagnostics.Process();
        process.StartInfo.FileName = "sc.exe";
        process.StartInfo.Arguments = "delete \"" + serviceName + "\"";
        process.StartInfo.CreateNoWindow = true;
        process.Start();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM