我爲我的Windows服務創建了一個安裝項目。 但是,如果我卸載該項目(通過添加/刪除程序,或右鍵單擊VS中的安裝項目 - 卸載),它安裝得很好,但似乎並沒有刪除該服務。卸載C#Windows服務 - 使用卸載程序
我必須在命令行使用sc delete來完成此操作,然後重新啓動。
我設置了錯誤嗎?
我爲我的Windows服務創建了一個安裝項目。 但是,如果我卸載該項目(通過添加/刪除程序,或右鍵單擊VS中的安裝項目 - 卸載),它安裝得很好,但似乎並沒有刪除該服務。卸載C#Windows服務 - 使用卸載程序
我必須在命令行使用sc delete來完成此操作,然後重新啓動。
我設置了錯誤嗎?
我不確定在安裝程序項目中是否有簡單的方法來完成此操作,但以下是代碼中的操作方法。我們的服務在內部通過命令行傳遞「/ uninstall」進行卸載。
public static readonly string UNINSTALL_REG_KEY = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";
public static readonly string UNINSTALL_REG_GUID = "{1C2301A...YOUR GUID HERE}";
using (RegistryKey parent = Registry.LocalMachine.OpenSubKey(UNINSTALL_REG_KEY, true))
{
try
{
RegistryKey key = null;
try
{
key = parent.OpenSubKey(UNINSTALL_REG_GUID, true);
if (key == null)
{
key = parent.CreateSubKey(UNINSTALL_REG_GUID);
}
Assembly asm = typeof (Service).Assembly;
Version v = asm.GetName().Version;
string exe = "\"" + asm.CodeBase.Substring(8).Replace("/", "\\\\") + "\"";
key.SetValue("DisplayName", DISPLAY_NAME);
key.SetValue("ApplicationVersion", v.ToString());
key.SetValue("Publisher", "Company Name");
key.SetValue("DisplayIcon", exe);
key.SetValue("DisplayVersion", v.ToString(2));
key.SetValue("URLInfoAbout", "http://www.company.com");
key.SetValue("Contact", "[email protected]");
key.SetValue("InstallDate", DateTime.Now.ToString("yyyyMMdd"));
key.SetValue("UninstallString", exe + " /uninstallprompt");
}
finally
{
if (key != null)
{
key.Close();
}
}
}
catch (Exception ex)
{
throw new Exception(
"An error occurred writing uninstall information to the registry. The service is fully installed but can only be uninstalled manually through the command line.",
ex);
}
}
在你Installer
類(從您的自定義操作調用),請確保您覆蓋UnInstall
方法,並呼籲<pathToFramework>\InstallUtil.exe /u <pathToServiceExe>
卸載該服務。
查看關於MDSN的ServiceInstaller文檔。您添加一個從System.Configuration.Install.Installer繼承的類,創建一個ServiceInstaller並將該ServiceInstaller添加到安裝程序屬性。
一旦你這樣做了,安裝程序項目應該能夠負責爲你安裝和卸載。安裝程序類的
例子:
/// <summary>
/// The installer class for the application
/// </summary>
[RunInstaller(true)]
public class MyInstaller : Installer
{
/// <summary>
/// Constructor for the installer
/// </summary>
public MyInstaller()
{
// Create the Service Installer
ServiceInstaller myInstaller = new ServiceInstaller();
myInstaller.DisplayName = "My Service";
myInstaller.ServiceName = "mysvc";
// Add the installer to the Installers property
Installers.Add(myInstaller);
}
}
難道我們的意見沒有提供任何這方面的幫助? http://stackoverflow.com/questions/1560407/c-windows-service-not-appearing-in-services-list-after-install – 2010-01-25 17:22:57
@Jon Seigel,看起來像對我來說是一個不同的問題。在原來的時候,他問的是如何讓他的服務在服務面板上顯示出來。在這裏,他問如何使它顯示在添加/刪除程序列表(或程序和現在稱爲的任何程序)中。 – 2010-01-25 17:27:05
好吧,是的 - 但它不包括卸載.... 它說,對於Windows 2000將需要重新啓動 - 但這是Windows 7! – Alex 2010-01-25 17:35:02