private void AutoSysService_Click(object sender, EventArgs e) { if (ServiceHelper.Status(strRegName).ToString() == "NotExist") { //安装 ServiceHelper.Install( strRegName, // 服务名 strRegName, // 显示名称 @"""" + Application.ExecutablePath + @""" service", // 映像路径,可带参数,若路径有空格,需给路径(不含参数)套上双引号 "Frp Windows客户端", // 服务描述 ServiceStartType.Auto, // 启动类型 ServiceAccount.LocalSystem, // 运行帐户,可选,默认是LocalSystem,即至尊帐户 null // 依赖服务,要填服务名称,没有则为null或空数组,可选 ); AutoSysService.Checked = RestartSysService.Enabled = StopSysService.Enabled = true; ServiceHelper.Restart(strRegName); ProcOutput.AppendText("已注册到系统服务,并启动(控制台无输出)..." + "\r\n"); } else { //卸载 ServiceHelper.Uninstall(strRegName); AutoSysService.Checked = RestartSysService.Enabled = StopSysService.Enabled = false; ProcOutput.AppendText("已删除系统服务,因系统机制,如重新注册需重启本程序(或exploere.exe)..." + "\r\n"); } }
private void OnMainFormLoad(object sender, EventArgs e) { System.IO.Directory.SetCurrentDirectory(System.Windows.Forms.Application.StartupPath); this.Text = this.Text + " - v" + Application.ProductVersion.ToString(); // 检查同目录多开 if (IsDuplicateInstance()) { MessageBox.Show("本程序已经在运行!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); Application.Exit(); } //检查是否管理员身份 WindowsPrincipal winPrincipal = new WindowsPrincipal(WindowsIdentity.GetCurrent()); bool isAdmin = winPrincipal.IsInRole(WindowsBuiltInRole.Administrator); if (isAdmin) { ProcOutput.AppendText("启动身份:管理员" + "\r\n"); // 判断服务状态 if (ServiceHelper.Status(strRegName).ToString() == "NotExist") { AutoSysService.Checked = RestartSysService.Enabled = StopSysService.Enabled = false; } else { AutoSysService.Checked = true; ProcOutput.AppendText($"已注册到系统服务,当前状态:{ServiceHelper.Status(strRegName).ToString()}" + "\r\n"); if (ServiceHelper.Status(strRegName).ToString() == "Running") { sStatus = true; } } } else { AutoSysService.Enabled = RestartSysService.Enabled = StopSysService.Enabled = false; ProcOutput.AppendText("启动身份:非管理员" + "\r\n"); } InitList(true); UpdateStartButton(); //notifyIcon.Icon = this.Icon; notifyIcon.Text = this.Text; //设置自启之后,开机启动要直接启动frp,并最小到托盘 if (AutoRun.Checked) { string[] strArgs = Environment.GetCommandLineArgs(); if (strArgs.Length >= 2 && strArgs[1].Equals(strAutoRun)) { this.Hide(); this.ShowInTaskbar = false; RestartService_Click(null, null); } } }
private void MyProcOutputHandler(object sendingProcess, DataReceivedEventArgs outLine) { if (!String.IsNullOrEmpty(outLine.Data)) { ProcOutput.AppendText(outLine.Data.ToString() + "\r\n"); ProcOutput.SelectionStart = ProcOutput.Text.Length; ProcOutput.ScrollToCaret(); } }
private void CloseFrp() { if (null == frp_process) { return; } frp_process.Kill(); frp_process.Close(); frp_process = null; ProcOutput.AppendText("停止服务..." + "\r\n"); bStatus = false; UpdateStartButton(); }
private void ReloadFrp() { if (DB.Instance().cServerinfo.nAdminPort > 0 && (bStatus || sStatus)) { Process frp_reload = new Process(); frp_reload.StartInfo.FileName = "frpc.exe"; frp_reload.StartInfo.Arguments = " reload -c " + DB.strFileName; frp_reload.StartInfo.CreateNoWindow = true; frp_reload.StartInfo.RedirectStandardOutput = true; frp_reload.OutputDataReceived += new DataReceivedEventHandler(MyProcOutputHandler); frp_reload.StartInfo.UseShellExecute = false; frp_reload.Start(); frp_reload.BeginOutputReadLine(); ProcOutput.AppendText("热加载配置文件..." + "\r\n"); } }
private void StopSysService_Click(object sender, EventArgs e) { ServiceHelper.Restart(strRegName, false); ProcOutput.AppendText($"已停止系统服务,当前状态:{ServiceHelper.Status(strRegName).ToString()}(控制台无输出)..." + "\r\n"); sStatus = false; }
private void RestartSysService_Click(object sender, EventArgs e) { ServiceHelper.Restart(strRegName); ProcOutput.AppendText($"已重启系统服务,当前状态:{ServiceHelper.Status(strRegName)}(控制台无输出)..." + "\r\n"); sStatus = true; }