Warm tip: This article is reproduced from serverfault.com, please click

c#-从Windows Service卸载软件

(c# - Uninstall Software from Windows Service)

发布于 2020-11-25 05:18:35

我在独立的Windows应用程序中具有以下代码,可使用注册表中的密钥来卸载程序。过去,我在很多地方都使用过此方法,并且一直有效,直到现在为止。

        using (Process p = new Process())
        {
            p.StartInfo.FileName = "MsiExec.exe";
            p.StartInfo.Arguments = "/x " + s.RegKey; // + " /qb";
            p.StartInfo.WorkingDirectory = @"C:\temp\";
            p.Start();
        }

它完美地工作。然后,我将相同的代码移到Windows服务中,如下所示。这是从服务器应用程序(Windows应用程序)传递到Kafka服务器并由Windows服务使用的KAFKA消息。

var readMessagesThread = new Thread(() =>
            {
                var consumerConfig = new ConsumerConfig
                {
                    GroupId = "Consumer-Group",
                    BootstrapServers = Global.KafkaServerURI,
                    MessageMaxBytes = Global.MessageMaxBytes,
                    AllowAutoCreateTopics = true,
                    FetchMaxBytes = Global.MessageMaxBytes
                };

                var cons = new ConsumerBuilder<Ignore, string>(consumerConfig).Build();
                cons.Subscribe("chat-topic");

                var cts = new CancellationTokenSource();
                Console.CancelKeyPress += (_, e) =>
                {
                    e.Cancel = true;
                    cts.Cancel();
                };

                try
                {
                    while (true)
                        try
                        {
                            ConsumeResult<Ignore, string> cr = cons.Consume(cts.Token);

                            string decryptedMessage = Encryption.DecryptString(Global.AUTH_KEY, cr.Message.Value);
                            Message deserializedMessage = JsonConvert.DeserializeObject<Message>(decryptedMessage);
                            
                            switch (deserializedMessage.MessageType)
                            {
                                case Global.MessageType.UninstallSoftware:
                                
                               ******  Call function which has the same lines of code as above **
                                Break;
                            }
                        }

                        catch (ConsumeException e)
                        {
                            Console.WriteLine(e);
                        }
                }
                catch (Exception e)
                {
                    cons.Close();
                }

            });

            readMessagesThread.Start();
        }

当我运行代码行以调用MSIEXEC时,什么都没有发生。我在三个不同的地方尝试了确切的代码,并一遍又一遍地使用了命令参数。

是因为它在服务中吗?是因为它在线程中吗?该服务作为LocalSystem帐户运行。

我花了20多个小时来尝试完成这项工作。为什么它在一个应用程序中而不在另一个应用程序中起作用?

任何帮助将不胜感激。

***** 2020年11月25日927 am ***好的,谢谢你通知我第0场...这很有帮助。现在,由于我已经可以使用PowerShell或DOS与命令提示符进行交互,因此我可以使用进程发送该命令

Questioner
Chris Dunlop
Viewed
0
Chris Dunlop 2020-12-04 04:17:31