1

I am developing a C# desktop application for my client that uses MySQL Server. What my client demands is that the database server should be started at the time of application start and also it should be stopped at the application exit. I know how to start a process in C# using Process.Start() but what I don't know is that how to detect the installation path of the MySQL server and start the server because the condition of my client is that the server installation path may change in future. Also I don't know how to stop a process/service in C#. So please tell me these two things:

  • Detect installation path of MySQL and start the server via C#
  • Stop the MySQL server on application exit via C#

I tried this but this code needs the path of the MySql installation. I need the path automatic and I am unable to predict any code for that

if (Process.GetProcessesByName("mysqld.exe") == null)
{
    Process.Start("C:\Program Files (x86)\MySQL\MySQL Cluster 5.5\bin\mysqld.exe");
}
4

2 回答 2

0

在 bin 文件夹中提供 MySQLInstanceConfig.exe 的路径...

"C:\Program Files\MySQL\MySQL 服务器 5.1\bin\mysqld"

通过以下链接可以帮助您

http://dev.mysql.com/doc/refman/5.0/en/mysql-config-wizard-starting.html

于 2012-09-24T10:53:33.630 回答
0

这完美无缺....只需转到您的 Windows 服务并确定机器上 MySQL 实例的服务名称。我的是'MySQL57'。超时只是为了确保服务状态在做其他事情之前返回预期值。

    public int RestartMySQL()
    {
        var serviceName = "MySQL57";
        var timeoutMilliseconds = 2000;

        var service = new ServiceController(serviceName, Environment.MachineName);

        try
        {
            TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds);

            if (service.Status == ServiceControllerStatus.Running)
            {
                service.Stop();
                service.WaitForStatus(ServiceControllerStatus.Stopped, timeout);
            }

            service.Start();
            service.WaitForStatus(ServiceControllerStatus.Running, timeout);

            return 5;
        }
        catch (Exception ex)
        {
            return 0;
        }
    }
于 2020-07-08T04:57:48.063 回答