C# ";错误1001";-指定的服务已存在。如何卸载该服务?

C# ";错误1001";-指定的服务已存在。如何卸载该服务?,c#,windows-services,windows-installer,C#,Windows Services,Windows Installer,我有一个安装Windows服务的项目安装程序。 创建新版本时,我需要卸载它并安装新版本。 请检查以下代码: private void InitializeComponent() { this.ServiceProcessInstaller = new System.ServiceProcess.ServiceProcessInstaller(); this.MyServiceInstaller = new System.ServiceProcess.ServiceInstalle

我有一个安装Windows服务的项目安装程序。
创建新版本时,我需要卸载它并安装新版本。
请检查以下代码:

private void InitializeComponent()
{
    this.ServiceProcessInstaller = new System.ServiceProcess.ServiceProcessInstaller();
    this.MyServiceInstaller = new System.ServiceProcess.ServiceInstaller();
    // 
    // ServiceProcessInstaller
    // 
    this.ServiceProcessInstaller.Account = System.ServiceProcess.ServiceAccount.LocalSystem;
    this.ServiceProcessInstaller.Password = null;
    this.ServiceProcessInstaller.Username = null;
    this.ServiceProcessInstaller.BeforeInstall += new System.Configuration.Install.InstallEventHandler(this.BeforeInstall);
    // 
    // MyServiceInstaller
    // 
    this.MyServiceInstaller.Description = "Description";
    this.MyServiceInstaller.ServiceName = "My Service";
    // 
    // ProjectInstaller
    // 
    this.Installers.AddRange(new System.Configuration.Install.Installer[] {
    this.ServiceProcessInstaller,
    this.MyServiceInstaller});

}


void BeforeInstall(object sender, System.Configuration.Install.InstallEventArgs e)
{
    List<ServiceController> services = new List<ServiceController>(ServiceController.GetServices());

    foreach (ServiceController s in services)
    {
        if (s.ServiceName == "My Service")
        {
            ServiceInstaller ServiceInstallerObj = new ServiceInstaller();
            ServiceInstallerObj.Context = new InstallContext();
            ServiceInstallerObj.ServiceName = "My Service";
            ServiceInstallerObj.Uninstall(null);

            break;
        }
    }
}
private void InitializeComponent()
{
this.ServiceProcessInstaller=新系统.ServiceProcess.ServiceProcessInstaller();
this.MyServiceInstaller=new System.ServiceProcess.ServiceInstaller();
// 
//ServiceProcessInstaller
// 
this.ServiceProcessInstaller.Account=System.ServiceProcess.ServiceAccount.LocalSystem;
this.ServiceProcessInstaller.Password=null;
this.ServiceProcessInstaller.Username=null;
this.ServiceProcessInstaller.BeforeInstall+=新系统.Configuration.Install.InstallEventHandler(this.BeforeInstall);
// 
//MyServiceInstaller
// 
this.MyServiceInstaller.Description=“Description”;
this.MyServiceInstaller.ServiceName=“我的服务”;
// 
//投射器
// 
this.Installers.AddRange(新的System.Configuration.Install.Installer[]{
此.ServiceProcessInstaller,
这是MyServiceInstaller});
}
安装前无效(对象发送方,System.Configuration.Install.InstallEventArgs e)
{
列表服务=新列表(ServiceController.GetServices());
foreach(服务中的服务控制器)
{
如果(s.ServiceName==“我的服务”)
{
ServiceInstaller ServiceInstallerObj=新的ServiceInstaller();
ServiceInstallerObj.Context=新的InstallContext();
ServiceInstallerObj.ServiceName=“我的服务”;
ServiceInstallerObj.Uninstall(空);
打破
}
}
}
我设置了BeforeInstall事件,但再次出现错误1001



接下来,这里有一些链接:。还有。现在,这里有一些链接:。然后是。