深圳全飞鸿

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 142|回复: 0
打印 上一主题 下一主题

C#程序MesServiceTool.exe的反编译代码

[复制链接]

228

主题

466

帖子

2184

积分

版主

Rank: 7Rank: 7Rank: 7

积分
2184
跳转到指定楼层
楼主
发表于 2025-2-21 22:42:02 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
  1. // MesServiceTool.Form1
  2. using System;
  3. using System.Collections;
  4. using System.ComponentModel;
  5. using System.Configuration.Install;
  6. using System.Drawing;
  7. using System.ServiceProcess;
  8. using System.Windows.Forms;

  9. public class Form1 : Form
  10. {
  11.         private IContainer components;

  12.         private Button btn_install;

  13.         private Button btn_uninstall;

  14.         private Button btn_start;

  15.         private Button btn_stop;

  16.         public Form1()
  17.         {
  18.                 InitializeComponent();
  19.         }

  20.         private void Form1_Load(object sender, EventArgs e)
  21.         {
  22.                 if (IsServiceExisted())
  23.                 {
  24.                         btn_install.Enabled = false;
  25.                         return;
  26.                 }
  27.                 btn_uninstall.Enabled = false;
  28.                 btn_start.Enabled = false;
  29.                 btn_stop.Enabled = false;
  30.         }

  31.         private void btn_install_Click(object sender, EventArgs e)
  32.         {
  33.                 if (InstallService())
  34.                 {
  35.                         MessageBox.Show("服务安装成功", "服务安装");
  36.                         btn_uninstall.Enabled = true;
  37.                         btn_start.Enabled = true;
  38.                         btn_stop.Enabled = true;
  39.                         btn_install.Enabled = false;
  40.                 }
  41.                 else
  42.                 {
  43.                         MessageBox.Show("服务安装失败", "服务安装");
  44.                 }
  45.         }

  46.         private void btn_uninstall_Click(object sender, EventArgs e)
  47.         {
  48.                 if (StopService() && UnInstallService())
  49.                 {
  50.                         MessageBox.Show("服务卸载成功", "服务卸载");
  51.                         btn_install.Enabled = true;
  52.                         btn_start.Enabled = false;
  53.                         btn_stop.Enabled = false;
  54.                         btn_uninstall.Enabled = false;
  55.                 }
  56.                 else
  57.                 {
  58.                         MessageBox.Show("服务卸载失败", "服务卸载");
  59.                 }
  60.         }

  61.         private void btn_start_Click(object sender, EventArgs e)
  62.         {
  63.                 if (StartService())
  64.                 {
  65.                         MessageBox.Show("服务开启成功", "服务开启");
  66.                 }
  67.                 else
  68.                 {
  69.                         MessageBox.Show("服务开启失败", "服务开启");
  70.                 }
  71.         }

  72.         private void btn_stop_Click(object sender, EventArgs e)
  73.         {
  74.                 if (StopService())
  75.                 {
  76.                         MessageBox.Show("服务停止成功", "服务停止");
  77.                 }
  78.                 else
  79.                 {
  80.                         MessageBox.Show("服务停止失败", "服务停止");
  81.                 }
  82.         }

  83.         private bool IsServiceExisted()
  84.         {
  85.                 ServiceController[] services = ServiceController.GetServices();
  86.                 for (int i = 0; i < services.Length; i++)
  87.                 {
  88.                         if (services[i].ServiceName.ToLower() == "LCMesService".ToLower())
  89.                         {
  90.                                 return true;
  91.                         }
  92.                 }
  93.                 return false;
  94.         }

  95.         private bool InstallService()
  96.         {
  97.                 try
  98.                 {
  99.                         using AssemblyInstaller assemblyInstaller = new AssemblyInstaller();
  100.                         assemblyInstaller.UseNewContext = true;
  101.                         assemblyInstaller.Path = Application.StartupPath + "\\MesService.exe";
  102.                         IDictionary dictionary = new Hashtable();
  103.                         assemblyInstaller.Install(dictionary);
  104.                         assemblyInstaller.Commit(dictionary);
  105.                 }
  106.                 catch (Exception)
  107.                 {
  108.                         return false;
  109.                 }
  110.                 return true;
  111.         }

  112.         private bool UnInstallService()
  113.         {
  114.                 try
  115.                 {
  116.                         using AssemblyInstaller assemblyInstaller = new AssemblyInstaller();
  117.                         assemblyInstaller.UseNewContext = true;
  118.                         assemblyInstaller.Path = Application.StartupPath + "\\MesService.exe";
  119.                         assemblyInstaller.Uninstall(null);
  120.                 }
  121.                 catch (Exception)
  122.                 {
  123.                         return false;
  124.                 }
  125.                 return true;
  126.         }

  127.         private bool StartService()
  128.         {
  129.                 try
  130.                 {
  131.                         using ServiceController serviceController = new ServiceController("LCMesService");
  132.                         if (serviceController.Status == ServiceControllerStatus.Stopped)
  133.                         {
  134.                                 serviceController.Start();
  135.                         }
  136.                 }
  137.                 catch (Exception)
  138.                 {
  139.                         return false;
  140.                 }
  141.                 return true;
  142.         }

  143.         private bool StopService()
  144.         {
  145.                 try
  146.                 {
  147.                         using ServiceController serviceController = new ServiceController("LCMesService");
  148.                         if (serviceController.Status == ServiceControllerStatus.Running)
  149.                         {
  150.                                 serviceController.Stop();
  151.                         }
  152.                 }
  153.                 catch (Exception)
  154.                 {
  155.                         return false;
  156.                 }
  157.                 return true;
  158.         }

  159.         protected override void Dispose(bool disposing)
  160.         {
  161.                 if (disposing && components != null)
  162.                 {
  163.                         components.Dispose();
  164.                 }
  165.                 base.Dispose(disposing);
  166.         }

  167.         private void InitializeComponent()
  168.         {
  169.                 btn_install = new System.Windows.Forms.Button();
  170.                 btn_uninstall = new System.Windows.Forms.Button();
  171.                 btn_start = new System.Windows.Forms.Button();
  172.                 btn_stop = new System.Windows.Forms.Button();
  173.                 SuspendLayout();
  174.                 btn_install.Location = new System.Drawing.Point(12, 34);
  175.                 btn_install.Name = "btn_install";
  176.                 btn_install.Size = new System.Drawing.Size(95, 31);
  177.                 btn_install.TabIndex = 0;
  178.                 btn_install.Text = "安装";
  179.                 btn_install.UseVisualStyleBackColor = true;
  180.                 btn_install.Click += new System.EventHandler(btn_install_Click);
  181.                 btn_uninstall.Location = new System.Drawing.Point(113, 34);
  182.                 btn_uninstall.Name = "btn_uninstall";
  183.                 btn_uninstall.Size = new System.Drawing.Size(95, 31);
  184.                 btn_uninstall.TabIndex = 1;
  185.                 btn_uninstall.Text = "卸载";
  186.                 btn_uninstall.UseVisualStyleBackColor = true;
  187.                 btn_uninstall.Click += new System.EventHandler(btn_uninstall_Click);
  188.                 btn_start.Location = new System.Drawing.Point(262, 34);
  189.                 btn_start.Name = "btn_start";
  190.                 btn_start.Size = new System.Drawing.Size(95, 31);
  191.                 btn_start.TabIndex = 2;
  192.                 btn_start.Text = "启动";
  193.                 btn_start.UseVisualStyleBackColor = true;
  194.                 btn_start.Click += new System.EventHandler(btn_start_Click);
  195.                 btn_stop.Location = new System.Drawing.Point(363, 34);
  196.                 btn_stop.Name = "btn_stop";
  197.                 btn_stop.Size = new System.Drawing.Size(95, 31);
  198.                 btn_stop.TabIndex = 3;
  199.                 btn_stop.Text = "停止";
  200.                 btn_stop.UseVisualStyleBackColor = true;
  201.                 btn_stop.Click += new System.EventHandler(btn_stop_Click);
  202.                 base.AutoScaleDimensions = new System.Drawing.SizeF(6f, 12f);
  203.                 base.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
  204.                 base.ClientSize = new System.Drawing.Size(465, 94);
  205.                 base.Controls.Add(btn_stop);
  206.                 base.Controls.Add(btn_start);
  207.                 base.Controls.Add(btn_uninstall);
  208.                 base.Controls.Add(btn_install);
  209.                 base.MaximizeBox = false;
  210.                 base.MinimizeBox = false;
  211.                 base.Name = "Form1";
  212.                 Text = "MesServer工具";
  213.                 base.Load += new System.EventHandler(Form1_Load);
  214.                 ResumeLayout(false);
  215.         }
  216. }
复制代码


回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|nagomes  

GMT+8, 2025-5-5 02:04 , Processed in 0.030106 second(s), 21 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表