简体   繁体   English

使用安装和部署以及Installer类卸载用C#编写的应用程序

[英]Uninstalling application written in c# using setup and deployment and Installer class

my program installs my custom service and register it. 我的程序将安装我的自定义服务并进行注册。 Now what I am trying to do is to unregister the service and delete all files after uninstall. 现在,我要尝试的是注销服务,并在卸载后删除所有文件。 I am using Visual Studio and Setup and Deployment and the Installer class, I have overridden a few methods that I am presenting below: 我正在使用Visual Studio,安装和部署以及Installer类,但是我已经覆盖了下面介绍的一些方法:

protected override void OnAfterUninstall(IDictionary savedState)
{
    base.OnAfterUninstall(savedState);
    string directory = "C:\\Program Files (x86)\\MyService\\";
    if (System.IO.Directory.Exists(directory))
    {
        string[] files = System.IO.Directory.GetFiles(directory);
        foreach (string file in files)
        {
            System.IO.File.Delete(file);
        }
        System.IO.Directory.Delete(directory);
    }
}

protected override void OnBeforeUninstall(IDictionary savedState)
{
    base.OnBeforeUninstall(savedState);
    string path = "-u \"C:\\Program Files (x86)\\MyService\\AA_service.exe\"";
    ProcessStartInfo startInfo = new ProcessStartInfo();
    startInfo.CreateNoWindow = false;
    startInfo.UseShellExecute = false;
    startInfo.FileName = "C:\\Program Files (x86)\\MyService\\InstallUtil.exe";
    startInfo.WindowStyle = ProcessWindowStyle.Hidden;
    startInfo.Arguments = path;


    Process.Start(startInfo);

}

It does not unregister service and it does not delete application folder. 它不会取消注册服务,也不会删除应用程序文件夹。 Can anyone suggest me what am I doing wrong? 谁能建议我我在做什么错?

//edit //编辑

now it is trying to remove files but I am getting access denied error on uninstall. 现在它正在尝试删除文件,但卸载时出现访问被拒绝的错误。 Files I am trying to delete are .exe, .dll and some others 我尝试删除的文件是.exe,.dll和其他一些文件

Did you add the custom actions into the MSI? 您是否已将自定义操作添加到MSI中? If you don't have a custom action fire for your uninstall event then I'm not sure if these events will be called. 如果您的卸载事件没有自定义操作触发,那么我不确定这些事件是否会被调用。 Is there any reason why you are using the before and after install events instead of overriding the "uninstall" command? 有什么理由为什么要使用安装前和安装后事件,而不是覆盖“卸载”命令?

If you don't call the Install function for the component, then the installer won't call the uninstall function either. 如果您不调用该组件的Install函数,则安装程序也不会调用该卸载函数。 You can program a messagebox into the uninstall (or a System.Diagnostics.Debugger.Attach() ) if you want to see whether the code is executing or not. 如果要查看代码是否正在执行,可以将消息框编程为卸载(或System.Diagnostics.Debugger.Attach() )。

Also as a matter of portability, I highly recommend that you use the Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles) command to find the program files directory. 另外,出于可移植性考虑,我强烈建议您使用Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles)命令查找程序文件目录。 This will port across x64 and x86 as well as any future revisions to the program files directory in the future. 这将在x64和x86以及将来对程序文件目录的任何将来修订版本中移植。

Secondly I would use the Path.Combine function as well to safely merge the folders together. 其次,我还将使用Path.Combine函数将文件夹安全地合并在一起。

EDIT: I think you may be misusing the installation procedure. 编辑:我认为您可能会滥用安装过程。 The custom action of the installer is to "register" the service once it has been installed by the MSI. 安装程序的自定义操作是在MSI安装该服务后对其进行“注册”。 It is also to unregister the service before it is then deleted by the MSI. 也要先注销该服务,然后再由MSI删除该服务。

Go get a copy of WIX or use the MSI builder in Visual Studio. 获取WIX的副本或使用Visual Studio中的MSI构建器。 Drop your project output for your service into the project, setup the custom actions to call on your service exe and then the MSI will handle the install/uninstall for you. 将服务的项目输出拖放到项目中,设置自定义操作以调用服务exe,然后MSI将为您处理安装/卸载。 THe custom action will be called and register/unregister your service with the cache. 自定义操作将被调用,并在缓存中注册/注销您的服务。

Be very careful though, if you need to upgrade there is a bug in the behaviour of the service installer, it is not able to succesfully upgrade or downgrade without you wiring the MSI properly to handle all the sequences that can occur. 但是,请务必小心,如果您需要升级,那么服务安装程序的行为将出现错误,如果您未正确连接MSI以处理可能发生的所有序列,则该服务无法成功升级或降级。

using system.Threading;

static void Main()
{
    string[] arguments = Environment.GetCommandLineArgs();
    foreach (string argument in arguments)
    {
        if (argument.Split('=')[0].ToLower() == "/u")
        {
            ThreadStart starter = delegate { WorkThreadFunction(argument.Split('=')[1]); };
            new Thread(starter).Start();
            Thread.Sleep(30000);
            Application.Exit();
            return;
        }
    }
}

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

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