专栏名称: SegmentFault思否
SegmentFault (www.sf.gg)开发者社区,是中国年轻开发者喜爱的极客社区,我们为开发者提供最纯粹的技术交流和分享平台。
目录
相关文章推荐
OSC开源社区  ·  Bun ... ·  2 天前  
程序员的那些事  ·  OpenAI ... ·  2 天前  
程序员小灰  ·  3个令人惊艳的DeepSeek项目,诞生了! ·  3 天前  
OSC开源社区  ·  2024: 大模型背景下知识图谱的理性回归 ·  5 天前  
程序员小灰  ·  DeepSeek做AI代写,彻底爆了! ·  6 天前  
51好读  ›  专栏  ›  SegmentFault思否

自做 Windows 上界面美观的 PHP 集成环境软件

SegmentFault思否  · 公众号  · 程序员  · 2017-09-22 12:35

正文

界面展示一下:

源码: SalamanderWnmp集成包下载

原因

平常工作中用Nginx比较多,网上虽然也有wnmp集成环境,但是感觉界面不好看,用起来不舒服,所有决定自己做一个吧。

原料

软件用的是C#,GUI框架是WPF(这个做出来更好看一点),先去官网下载PHP,用的是NTS版本的(因为这里PHP是以CGi的形式跑的),再去下载Windows版的Nginx和Mysql

代码

基类

  1.    public class WnmpProgram: INotifyPropertyChanged

  2.        {

  3.            public TextBlock statusLabel { get; set; } // Label that shows the programs status

  4.            public string exeName { get; set; }    // Location of the executable file

  5.            public string procName { get; set; }   // Name of the process

  6.            public string progName { get; set; }   // User-friendly name of the program

  7.            public string workingDir { get; set; }   // working directory

  8.            public Log.LogSection progLogSection { get; set; } // LogSection of the program

  9.            public string startArgs { get; set; }  // Start Arguments

  10.            public string stopArgs { get; set; }   // Stop Arguments if KillStop is false

  11.            public bool killStop { get; set; }     // Kill process instead of stopping it gracefully

  12.            public string confDir { get; set; }    // Directory where all the programs configuration files are

  13.            public string logDir { get; set; }     // Directory where all the programs log files are

  14.            public Ini Settings { get; set; }

  15.            //public ContextMenuStrip configContextMenu { get; set; } // Displays all the programs config files in |confDir|

  16.            //public ContextMenuStrip logContextMenu { get; set; }    // Displays all the programs log files in |logDir|

  17.            public Process ps = new Process();

  18.            public event PropertyChangedEventHandler PropertyChanged;

  19.            // 是否在运行

  20.            private bool running = false;

  21.            public bool Running

  22.            {

  23.                get

  24.                {

  25.                    return this.running;

  26.                }

  27.                set

  28.                {

  29.                     this.running = value;

  30.                    if(PropertyChanged != null)

  31.                    {

  32.                        PropertyChanged(this, new PropertyChangedEventArgs("Running"));

  33.                    }

  34.                }

  35.            }

  36.            public WnmpProgram()

  37.            {

  38.                 //configContextMenu = new ContextMenuStrip();

  39.                //logContextMenu = new ContextMenuStrip();

  40.                //configContextMenu.ItemClicked += configContextMenu_ItemClicked;

  41.                //logContextMenu.ItemClicked += logContextMenu_ItemClicked;

  42.            }

  43.            ///

  44.            /// 设置状态

  45.            ///

  46.            public void SetStatus()

  47.            {

  48.                if (this.IsRunning() == true)

  49.                {

  50.                    this.Running = true;

  51.                }

  52.                 else

  53.                {

  54.                    this.Running = false;

  55.                }

  56.            }

  57.            public void StartProcess(string exe, string args, bool wait = false)

  58.            {

  59.                ps.StartInfo.FileName = exe;

  60.                ps.StartInfo.Arguments = args;

  61.                ps.StartInfo.UseShellExecute = false;

  62.                ps.StartInfo.RedirectStandardOutput = true;

  63.                ps. StartInfo.WorkingDirectory = workingDir;

  64.                ps.StartInfo.CreateNoWindow = true;

  65.                ps.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;

  66.                ps.Start();

  67.                 if (wait) {

  68.                    ps.WaitForExit();

  69.                }

  70.            }

  71.            public virtual void Start()

  72.            {

  73.                 if(IsRunning())

  74.                {

  75.                    return;

  76.                }

  77.                try {

  78.                    StartProcess(exeName, startArgs);

  79.                    Log.wnmp_log_notice("Started " + progName, progLogSection);

  80.                } catch (Exception ex) {

  81.                    Log.wnmp_log_error("Start(): " + ex.Message, progLogSection);

  82.                }

  83.            }

  84.            public virtual void Stop()

  85.            {

  86.                if(!IsRunning())

  87.                {

  88.                     return;

  89.                }

  90.                if (killStop == false)

  91.                    StartProcess(exeName, stopArgs, true);

  92.                var processes = Process.GetProcessesByName(procName);

  93.                 foreach (var process in processes) {

  94.                        process.Kill();

  95.                }

  96.                Log.wnmp_log_notice("Stopped " + progName, progLogSection);

  97.            }

  98.            public void Restart()

  99.            {

  100.                this.Stop();

  101.                this.Start();

  102.                 Log.wnmp_log_notice("Restarted " + progName, progLogSection);

  103.            }

  104.            //public void ConfigButton(object sender)

  105.            //{

  106.             //    var btnSender = (Button)sender;

  107.            //    var ptLowerLeft = new Point(0, btnSender.Height);

  108.            //    ptLowerLeft = btnSender.PointToScreen(ptLowerLeft);

  109.            //    configContextMenu.Show(ptLowerLeft);

  110.             //}

  111.            //private void logContextMenu_ItemClicked(object sender, ToolStripItemClickedEventArgs e)

  112.            //{

  113.            //    try {

  114.             //        Process.Start(Settings.Editor.Value, frmMain.StartupPath + logDir + e.ClickedItem.Text);

  115.            //    } catch (Exception ex) {

  116.            //        Log.wnmp_log_error(ex.Message, progLogSection);

  117.            //    }

  118.             //}

  119.            public bool IsRunning()

  120.            {

  121.                 var processes = Process.GetProcessesByName(procName);

  122.                return (processes.Length != 0);

  123.            }

  124.        }

开启mysql代码

  1.    class MysqlProgram : WnmpProgram

  2.        {

  3.             private readonly ServiceController MysqlController = new ServiceController();

  4.            public const string ServiceName = "mysql-salamander";

  5.             public MysqlProgram()

  6.            {

  7.                MysqlController.MachineName = Environment.MachineName;

  8.                MysqlController. ServiceName = ServiceName;

  9.            }

  10.            public void RemoveService()

  11.            {

  12.                StartProcess("cmd.exe", stopArgs, true);

  13.            }

  14.            public void InstallService()

  15.            {

  16.                StartProcess(exeName, startArgs, true);

  17.            }

  18.            public bool ServiceExists()

  19.            {

  20.                ServiceController[] services = ServiceController.GetServices();

  21.                foreach (var service in services) {

  22.                     if (service.ServiceName == ServiceName)

  23.                        return true;

  24.                }

  25.                 return false;

  26.            }

  27.             public override void Start()

  28.            {

  29.                if(MysqlController.Status == ServiceControllerStatus.Running)

  30.                {

  31.                    return;

  32.                }

  33.                 try {

  34.                    MysqlController.Start();

  35.                    MysqlController.WaitForStatus( ServiceControllerStatus.Running);

  36.                    Log.wnmp_log_notice("Started " + progName, progLogSection);

  37.                } catch (Exception ex) {

  38.                     Log.wnmp_log_error("Start(): " + ex.Message, progLogSection);

  39.                }

  40.            }

  41.            public override void Stop()

  42.            {

  43.                 if(MysqlController.Status == ServiceControllerStatus.Stopped)

  44.                {

  45.                    return;

  46.                }

  47.                try {

  48.                    MysqlController.Stop();

  49.                     MysqlController.WaitForStatus(ServiceControllerStatus.Stopped);

  50.                    Log.wnmp_log_notice("Stopped " + progName, progLogSection);

  51.                } catch (Exception ex) {

  52.                    Log.wnmp_log_notice(







请到「今天看啥」查看全文