卸载Windows服务的正确方法?

发布于 2024-09-13 00:53:06 字数 764 浏览 7 评论 0原文

我有一个使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

顾挽 2024-09-20 00:53:06

为了任何寻求这些问题答案的人的利益:

卸载之前不会停止服务

尚未找到解决方案。

卸载后未删除安装文件夹

需要覆盖项目安装程序中的 OnAfterUninstall 方法,并且必须删除任何创建的文件。如果执行此步骤后应用程序安装程序文件夹不包含任何文件,则会自动删除该文件夹。

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.
}

请记住,安装文件夹必须作为参数传入:

  • 右键单击您的安装项目,然后选择“查看”->“查看”。自定义操作
  • 自定义操作将在您的主窗口中打开。右键单击“卸载”节点下的“来自 XXX 的主输出”,然后选择“属性窗口”。
  • 在属性窗口中的 CustomActionData 下,输入以下内容:/TargetDir="[TARGETDIR]|"(请注意管道位于末端,请勿将其移除)。

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

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.

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
  • The custom actions will open in your main window. Right click on "Primary output from XXX" under the Uninstall node and select "Properties Window"
  • In the properties window, under CustomActionData, enter the following: /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.

两人的回忆 2024-09-20 00:53:06

最有可能的是,服务只是需要一点时间来关闭,而您在服务完全停止之前继续运行。尝试调用 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.

掐死时间 2024-09-20 00:53:06

只要按照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();

Just follow Mun's answer, by adding the following code in the "OnAfterUninstall" function, the Windows service will be deleted.

        var process = new System.Diagnostics.Process();
        process.StartInfo.FileName = "sc.exe";
        process.StartInfo.Arguments = "delete \"" + serviceName + "\"";
        process.StartInfo.CreateNoWindow = true;
        process.Start();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文