公司设计网站多少钱,邢台做wap网站多少钱,加工网袋的设备多少钱,17网站一起做网店 睡衣目录 一、前言
二、效果图
三、配置文件
四、代码
五、一键启动/停止所有服务 一、前言
使用C#窗体应用开启Apache、MySQL服务#xff0c;不仅仅是Apache、MySQL#xff0c;其他服务也可以使用同样的方法操作#xff0c;包括开启自己写的脚本服务。
二、效果图 两种状…目录 一、前言
二、效果图
三、配置文件
四、代码
五、一键启动/停止所有服务 一、前言
使用C#窗体应用开启Apache、MySQL服务不仅仅是Apache、MySQL其他服务也可以使用同样的方法操作包括开启自己写的脚本服务。
二、效果图 两种状态 ① 开启Apache点击启动按钮开启apache同时服务状态切换为运行中 ② 停止apache点击停止按钮停止apache同时服务状态切换为已停止 三、配置文件
用于存储需要操作的服务的路径和服务命要根据实际情况修改存放在”\bin\Debug\“目录下命名为app.ini
[section]
apachehttpd
mysqlmysqld
pythonStartService.vbs
python2Python
apache_pathE:\phpstudy_pro\Extensions\Apache2.4.39\bin\httpd.exe
mysql_pathE:\phpstudy_pro\Extensions\MySQL5.7.26\bin\mysqld.exe
python_pathE:\Pyhton\flow\
四、代码
这里只展示开启和关闭Apache服务的代码其他服务操作的代码一样就不再展示了只需要把服务名和服务所在路径替换就可以了。 业务逻辑 需要先读取配置文件app.ini文件里的内容 ① 开启服务获取到apache的路径构建启动信息开始启动apache服务。 ② 关闭服务先获取apache的服务名注意是服务名不是路径再查找名为apache的进程找到后关闭该服务为了确保确实关闭了在2秒后使用Kill()强制关闭。 读取配置文件的参数及用法可以看这篇文章
【C#】读取ini配置文件的内容_c#读取ini配置文件-CSDN博客
//配置文件路径
private string iniFilePath Directory.GetCurrentDirectory() \\app.ini;#region API函数声明
[DllImport(kernel32)]//返回取得字符串缓冲区的长度
private static extern long GetPrivateProfileString(string section, string key,string def, StringBuilder retVal, int size, string filePath);#endregion#region 读Ini文件public static string ReadIniData(string Section, string Key, string NoText, string iniFilePath)
{if (File.Exists(iniFilePath)){StringBuilder temp new StringBuilder(1024);GetPrivateProfileString(Section, Key, NoText, temp, 1024, iniFilePath);return temp.ToString();}else{return String.Empty;}
}// 开启apache服务
private void apache_start_Click(object sender, EventArgs e)
{string apache_path ReadIniData(section, apache_path, NoText, iniFilePath);string StartScript apache_path;// 构建启动信息ProcessStartInfo startInfo new ProcessStartInfo{FileName StartScript,CreateNoWindow true, // 不创建窗口UseShellExecute false, // 不使用系统外壳程序启动WindowStyle ProcessWindowStyle.Hidden // 隐藏窗口};// 启动Apacheusing (Process apacheProcess Process.Start(startInfo)){// 你可以在这里监控进程或等待它结束apache_status.Text 运行中;apache_status.ForeColor System.Drawing.Color.Green;}
}
// 停止apache服务
private void apache_stop_Click(object sender, EventArgs e)
{string apache ReadIniData(section, apache, NoText, iniFilePath);try{// 获取所有名为 apache 的进程 Process[] processes Process.GetProcessesByName(apache);foreach (Process process in processes){try{// 尝试优雅地关闭进程如果可能的话 // 注意apache可能没有主窗口因此CloseMainWindow可能不起作用 process.CloseMainWindow();// 等待一段时间看进程是否关闭 if (!process.WaitForExit(2000)) // 等待5秒 {// 如果进程没有关闭则强制结束它 process.Kill();}apache_status.Text 已停止;apache_status.ForeColor System.Drawing.Color.Crimson;}catch (Exception ex){// 处理任何异常比如权限不足等 MessageBox.Show(Error closing process: ex.Message);}}MessageBox.Show(Apache services have been closed.);}catch (Exception ex){// 处理获取进程列表时的任何异常 MessageBox.Show(Error getting process list: ex.Message);}
}
五、一键启动/停止所有服务
一键启动/关闭所有服务代码就不再展示其原理是把需要开启的服务放到同一个点击事件里比如说一键开启Apache、MySQL服务只需要把开启Apache的代码和开启MySQL服务的代码放到同一个点击事件里就可以了。一键停止所有服务也是同样的道理就不再贴代码了。