日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

C#开发人员能够可视化操作windows服务

發布時間:2025/3/17 C# 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C#开发人员能够可视化操作windows服务 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

使用C#開發自己的定義windows服務是一個很簡單的事。因此,當。我們需要發展自己windows它的服務。這是當我們需要有定期的計算機或運行某些程序的時候,我們開發。在這里,我有WCF監聽案例,因為我是做本地聊天室。必須server結束監控終端,所以,我開發了一個服務。便控制此監聽服務。

然而。我們開發的windows服務,默認情況下是無法可視化的操作的。這里我就額外的開發一個工具來對此服務進行操作,效果圖例如以下:

開發步驟:

1、“新建項目”——“Window服務”

Program.cs代碼:

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ServiceModel; using System.ServiceModel.Description; using System.ServiceProcess;namespace MSMQChatService {class Program{static void Main(){#region 服務啟動入口。正式用ServiceBase[] ServicesToRun;ServicesToRun = new ServiceBase[] { new MQChatService() };ServiceBase.Run(ServicesToRun);#endregion}}

MQChatService.cs代碼例如以下:

protected override void OnStart(string[] args){//開啟服務 這里就是你想要讓服務做的操作StartService();}

3、切換到MQChatService的可視化界面

4、在可視化界面,單擊鼠標右鍵,

將會出現一個Installer為后綴的新界面。默認好像是Project Installer.cs,我這里將其重命名為ServiceInstaller.cs

分別對界面上這兩個組件進行屬性配置,詳細的屬性簽名能夠查看屬性面板的最以下(右下角處)


好了,我們的windows服務已經開發好了。接下來就開發一個可視化的控制器,來控制服務的安裝、卸載、啟動和停止。

1、? 新建一個windows程序,名稱ServiceSetup。Form1重命名為FrmServiceSetup,

界面控件例如以下:


Program.cs代碼例如以下:

using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms;namespace ServiceSetup {static class Program{/// <summary>/// 應用程序的主入口點。/// </summary>[STAThread]static void Main(){//獲取欲啟動進程名string strProcessName = System.Diagnostics.Process.GetCurrentProcess().ProcessName;獲取版本//CommonData.VersionNumber = Application.ProductVersion;//檢查進程是否已經啟動,已經啟動則顯示報錯信息退出程序。

??????????? if (System.Diagnostics.Process.GetProcessesByName(strProcessName).Length > 1) ??????????? { ??????????????? MessageBox.Show("程序已經執行。"); ??????????????? Thread.Sleep(1000); ??????????????? System.Environment.Exit(1); ??????????? } ??????????? else ??????????? { ??????????????? Application.EnableVisualStyles(); ??????????????? Application.SetCompatibleTextRenderingDefault(false); ??????????????? Application.Run(new FrmServiceSetup()); ??????????? } } } }

主界面代碼:

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms;namespace ServiceSetup {public partial class FrmServiceSetup : Form{string strServiceName = string.Empty;public FrmServiceSetup(){InitializeComponent();strServiceName = string.IsNullOrEmpty(lblServiceName.Text) ? "MSMQChatService" : lblServiceName.Text;InitControlStatus(strServiceName, btnInstallOrUninstall, btnStartOrEnd, btnGetStatus, lblMsg, gbMain);}/// <summary>/// 初始化控件狀態/// </summary>/// <param name="serviceName">服務名稱</param>/// <param name="btn1">安裝button</param>/// <param name="btn2">啟動button</param>/// <param name="btn3">獲取狀態button</param>/// <param name="txt">提示信息</param>/// <param name="gb">服務所在組合框</param>void InitControlStatus(string serviceName, Button btn1, Button btn2, Button btn3, Label txt, GroupBox gb){try{btn1.Enabled = true;if (ServiceAPI.isServiceIsExisted(serviceName)){btn3.Enabled = true;btn2.Enabled = true;btn1.Text = "卸載服務";int status = ServiceAPI.GetServiceStatus(serviceName);if (status == 4){btn2.Text = "停止服務";}else{btn2.Text = "啟動服務";}GetServiceStatus(serviceName, txt);//獲取服務版本號string temp = string.IsNullOrEmpty(ServiceAPI.GetServiceVersion(serviceName)) ? string.Empty : "(" + ServiceAPI.GetServiceVersion(serviceName) + ")";gb.Text += temp;}else{btn1.Text = "安裝服務";btn2.Enabled = false;btn3.Enabled = false;txt.Text = "服務【" + serviceName + "】未安裝!";}}catch (Exception ex){txt.Text = "error";LogAPI.WriteLog(ex.Message);}}/// <summary>/// 安裝或卸載服務/// </summary>/// <param name="serviceName">服務名稱</param>/// <param name="btnSet">安裝、卸載</param>/// <param name="btnOn">啟動、停止</param>/// <param name="txtMsg">提示信息</param>/// <param name="gb">組合框</param>void SetServerce(string serviceName, Button btnSet, Button btnOn, Button btnShow, Label txtMsg, GroupBox gb){try{string location = System.Reflection.Assembly.GetExecutingAssembly().Location;string serviceFileName = location.Substring(0, location.LastIndexOf('\\')) + "\\" + serviceName + ".exe";if (btnSet.Text == "安裝服務"){ServiceAPI.InstallmyService(null, serviceFileName);if (ServiceAPI.isServiceIsExisted(serviceName)){txtMsg.Text = "服務【" + serviceName + "】成功安裝!";btnOn.Enabled = btnShow.Enabled = true;string temp = string.IsNullOrEmpty(ServiceAPI.GetServiceVersion(serviceName)) ?

string.Empty : "(" + ServiceAPI.GetServiceVersion(serviceName) + ")"; gb.Text += temp; btnSet.Text = "卸載服務"; btnOn.Text = "啟動服務"; } else { txtMsg.Text = "服務【" + serviceName + "】安裝失敗,請檢查日志。"; } } else { ServiceAPI.UnInstallmyService(serviceFileName); if (!ServiceAPI.isServiceIsExisted(serviceName)) { txtMsg.Text = "服務【" + serviceName + "】卸載成功!"; btnOn.Enabled = btnShow.Enabled = false; btnSet.Text = "安裝服務"; //gb.Text =strServiceName; } else { txtMsg.Text = "服務【" + serviceName + "】卸載失敗。請檢查日志!"; } } } catch (Exception ex) { txtMsg.Text = "error"; LogAPI.WriteLog(ex.Message); } } //獲取服務狀態 void GetServiceStatus(string serviceName, Label txtStatus) { try { if (ServiceAPI.isServiceIsExisted(serviceName)) { string statusStr = ""; int status = ServiceAPI.GetServiceStatus(serviceName); switch (status) { case 1: statusStr = "服務未執行!

"; break; case 2: statusStr = "服務正在啟動!"; break; case 3: statusStr = "服務正在停止!

"; break; case 4: statusStr = "服務正在執行。"; break; case 5: statusStr = "服務即將繼續。"; break; case 6: statusStr = "服務即將暫停!"; break; case 7: statusStr = "服務已暫停!

"; break; default: statusStr = "未知狀態!"; break; } txtStatus.Text = statusStr; } else { txtStatus.Text = "服務【" + serviceName + "】未安裝!"; } } catch (Exception ex) { txtStatus.Text = "error"; LogAPI.WriteLog(ex.Message); } } //啟動服務 void OnService(string serviceName, Button btn, Label txt) { try { if (btn.Text == "啟動服務") { ServiceAPI.RunService(serviceName); int status = ServiceAPI.GetServiceStatus(serviceName); if (status == 2 || status == 4 || status == 5) { txt.Text = "服務【" + serviceName + "】啟動成功!"; btn.Text = "停止服務"; } else { txt.Text = "服務【" + serviceName + "】啟動失敗!"; } } else { ServiceAPI.StopService(serviceName); int status = ServiceAPI.GetServiceStatus(serviceName); if (status == 1 || status == 3 || status == 6 || status == 7) { txt.Text = "服務【" + serviceName + "】停止成功!"; btn.Text = "啟動服務"; } else { txt.Text = "服務【" + serviceName + "】停止失敗。"; } } } catch (Exception ex) { txt.Text = "error"; LogAPI.WriteLog(ex.Message); } } //安裝or卸載服務 private void btnInstallOrUninstall_Click(object sender, EventArgs e) { btnInstallOrUninstall.Enabled = false; SetServerce(strServiceName, btnInstallOrUninstall, btnStartOrEnd, btnGetStatus, lblMsg, gbMain); btnInstallOrUninstall.Enabled = true; btnInstallOrUninstall.Focus(); } //啟動or停止服務 private void btnStartOrEnd_Click(object sender, EventArgs e) { btnStartOrEnd.Enabled = false; OnService(strServiceName, btnStartOrEnd, lblMsg); btnStartOrEnd.Enabled = true; btnStartOrEnd.Focus(); } //獲取服務狀態 private void btnGetStatus_Click(object sender, EventArgs e) { btnGetStatus.Enabled = false; GetServiceStatus(strServiceName, lblMsg); btnGetStatus.Enabled = true; btnGetStatus.Focus(); } private void FrmServiceSetup_Resize(object sender, EventArgs e) { if (this.WindowState == FormWindowState.Minimized) //最小化到系統托盤 { notifyIcon1.Visible = true; //顯示托盤圖標 this.ShowInTaskbar = false; this.Hide(); //隱藏窗口 } } private void FrmServiceSetup_FormClosing(object sender, FormClosingEventArgs e) { DialogResult result = MessageBox.Show("是縮小到托盤?", "確認", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Information); if (result== DialogResult.Yes) { // 取消關閉窗口 e.Cancel = true; // 將窗口變為最小化 this.WindowState = FormWindowState.Minimized; } else if (result == DialogResult.No) { System.Environment.Exit(0); } else { e.Cancel = true; } } private void notifyIcon1_MouseClick(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left&&this.WindowState == FormWindowState.Minimized) { this.Show(); this.WindowState = FormWindowState.Normal; this.ShowInTaskbar = true; //顯示在系統任務欄 //notifyIcon1.Visible = false; //托盤圖標不可見 this.Activate(); } } private void 打開主界面ToolStripMenuItem_Click(object sender, EventArgs e) { this.Show(); this.WindowState = FormWindowState.Normal; this.ShowInTaskbar = true; //顯示在系統任務欄 notifyIcon1.Visible = false; //托盤圖標不可見 this.Activate(); } private void 退出ToolStripMenuItem_Click(object sender, EventArgs e) { System.Environment.Exit(0); ExitProcess(); } ??? //結束進程 ??????? private void ExitProcess() ??????? { ??????????? System.Environment.Exit(0); ??????????? Process[] ps = Process.GetProcesses(); ??????????? foreach (Process item in ps) ??????????? { ??????????????? if (item.ProcessName == "ServiceSetup") ??????????????? { ??????????????????? item.Kill(); ??????????????? } ??????????? } ??????? } ?} }

新建一個類,專門用于日志操作LogAPI.cs

using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks;namespace ServiceSetup {public class LogAPI{private static string myPath = "";private static string myName = "";/// <summary>/// 初始化日志文件/// </summary>/// <param name="logPath"></param>/// <param name="logName"></param>public static void InitLogAPI(string logPath, string logName){myPath = logPath;myName = logName;}/// <summary>/// 寫入日志/// </summary>/// <param name="ex">日志信息</param>public static void WriteLog(string ex){if (myPath == "" || myName == "")return;string Year = DateTime.Now.Year.ToString();string Month = DateTime.Now.Month.ToString().PadLeft(2, '0');string Day = DateTime.Now.Day.ToString().PadLeft(2, '0');//年月日文件夾是否存在,不存在則建立if (!Directory.Exists(myPath + "\\LogFiles\\" + Year + "_" + Month + "\\" + Year + "_" + Month + "_" + Day)){Directory.CreateDirectory(myPath + "\\LogFiles\\" + Year + "_" + Month + "\\" + Year + "_" + Month + "_" + Day);}//寫入日志UNDO,Exception has not been handlestring LogFile = myPath + "\\LogFiles\\" + Year + "_" + Month + "\\" + Year + "_" + Month + "_" + Day + "\\" + myName;if (!File.Exists(LogFile)){System.IO.StreamWriter myFile;myFile = System.IO.File.AppendText(LogFile);myFile.Close();}while (true){try{StreamWriter sr = File.AppendText(LogFile);sr.WriteLine(DateTime.Now.ToString("HH:mm:ss") + " " + ex);sr.Close();break;}catch (Exception e){System.Threading.Thread.Sleep(50);continue;}}}} } Windows服務的操作類ServiceAPI.csusing System; using System.Collections; using System.Collections.Generic; using System.Configuration.Install; using System.IO; using System.Linq; using System.Reflection; using System.ServiceProcess; using System.Text; using System.Threading.Tasks; using Microsoft.Win32;namespace ServiceSetup {public class ServiceAPI{/// <summary>/// 檢查服務存在的存在性/// </summary>/// <param name=" NameService ">服務名</param>/// <returns>存在返回 true,否則返回 false;</returns>public static bool isServiceIsExisted(string NameService){ServiceController[] services = ServiceController.GetServices();foreach (ServiceController s in services){if (s.ServiceName.ToLower() == NameService.ToLower()){return true;}}return false;}/// <summary>/// 安裝Windows服務/// </summary>/// <param name="stateSaver">集合</param>/// <param name="filepath">程序文件路徑</param>public static void InstallmyService(IDictionary stateSaver, string filepath){AssemblyInstaller AssemblyInstaller1 = new AssemblyInstaller();AssemblyInstaller1.UseNewContext = true;AssemblyInstaller1.Path = filepath;AssemblyInstaller1.Install(stateSaver);AssemblyInstaller1.Commit(stateSaver);AssemblyInstaller1.Dispose();}/// <summary>/// 卸載Windows服務/// </summary>/// <param name="filepath">程序文件路徑</param>public static void UnInstallmyService(string filepath){AssemblyInstaller AssemblyInstaller1 = new AssemblyInstaller();AssemblyInstaller1.UseNewContext = true;AssemblyInstaller1.Path = filepath;AssemblyInstaller1.Uninstall(null);AssemblyInstaller1.Dispose();}/// <summary>/// 啟動服務/// </summary>/// <param name=" NameService ">服務名</param>/// <returns>存在返回 true,否則返回 false;</returns>public static bool RunService(string NameService){bool bo = true;try{ServiceController sc = new ServiceController(NameService);if (sc.Status.Equals(ServiceControllerStatus.Stopped) || sc.Status.Equals(ServiceControllerStatus.StopPending)){sc.Start();}}catch (Exception ex){bo = false;LogAPI.WriteLog(ex.Message);}return bo;}/// <summary>/// 停止服務/// </summary>/// <param name=" NameService ">服務名</param>/// <returns>存在返回 true,否則返回 false;</returns>public static bool StopService(string NameService){bool bo = true;try{ServiceController sc = new ServiceController(NameService);if (!sc.Status.Equals(ServiceControllerStatus.Stopped)){sc.Stop();}}catch (Exception ex){bo = false;LogAPI.WriteLog(ex.Message);}return bo;}/// <summary>/// 獲取服務狀態/// </summary>/// <param name=" NameService ">服務名</param>/// <returns>返回服務狀態</returns>public static int GetServiceStatus(string NameService){int ret = 0;try{ServiceController sc = new ServiceController(NameService);ret = Convert.ToInt16(sc.Status);}catch (Exception ex){ret = 0;LogAPI.WriteLog(ex.Message);}return ret;}/// <summary>/// 獲取服務安裝路徑/// </summary>/// <param name="ServiceName"></param>/// <returns></returns>public static string GetWindowsServiceInstallPath(string ServiceName){string path = "";try{string key = @"SYSTEM\CurrentControlSet\Services\" + ServiceName;path = Registry.LocalMachine.OpenSubKey(key).GetValue("ImagePath").ToString();path = path.Replace("\"", string.Empty);//替換掉雙引號 FileInfo fi = new FileInfo(path);path = fi.Directory.ToString();}catch (Exception ex){path = "";LogAPI.WriteLog(ex.Message);}return path;}/// <summary>/// 獲取指定服務的版本/// </summary>/// <param name="serviceName">服務名稱</param>/// <returns></returns>public static string GetServiceVersion(string serviceName){if (string.IsNullOrEmpty(serviceName)){return string.Empty;}try{string path = GetWindowsServiceInstallPath(serviceName) + "\\" + serviceName + ".exe";Assembly assembly = Assembly.LoadFile(path);AssemblyName assemblyName = assembly.GetName();Version version = assemblyName.Version;return version.ToString();}catch (Exception ex){LogAPI.WriteLog(ex.Message);return string.Empty;}}} } 注意:記住服務程序dll復制到可視化的安裝程序bin下面的文件夾。

版權聲明:本文博客原創文章,博客,未經同意,不得轉載。

總結

以上是生活随笔為你收集整理的C#开发人员能够可视化操作windows服务的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。