Sending commands to cmd prompt in C# with Windows Service

33 Views Asked by At

First of all, I would like to point out that my knowledge of English is low.

In the Windows Service project I wrote with .Net Framework 4.5, there is a system in which I install the program by taking parameters from the cmd prompt. I can install or remove the service with the parameters sent, but I cannot send a message back to the command line.

        static void Main(string[] args)
        {
            if (System.Environment.UserInteractive)
            {
                string executablePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "ErdatAktar.exe");
                if (args.Length > 0)
                {
                    try
                    {
                        switch (args[0])
                        {
                            case "-install":
                                {
                                    // Yönetici olarak çalıştırıldığından emin ol
                                    if (!IsUserAdministrator())
                                    {
                                        Console.WriteLine("Administrator rights are required for this operation.");
                                        return;
                                    }


                                    ManagedInstallerClass.InstallHelper(new string[] { executablePath });
                                    Console.WriteLine("The service has been successfully installed.");
                                    break;
                                }
                            case "-uninstall":
                                {
                                    Console.WriteLine("The service was removed successfully.");
                                    ManagedInstallerClass.InstallHelper(new string[] { "/u", executablePath });
                                    break;
                                }
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.ToString());
                    }
                }
            }
            else
            {
                ServiceBase[] ServicesToRun;
                ServicesToRun = new ServiceBase[] { new ErdatAktar() };
                ServiceBase.Run(ServicesToRun);
            }

        }
0

There are 0 best solutions below