天下网吧 >> 网吧天地 >> 天下码农 >> 桌面开发 >> C# >> 正文

C#边学边玩之修复网吧火箭下载器源码,C#操作系统服务、运行外部程序源码参考

上一篇:

C#操作SQLite教程,使用NuGet添加SQLite的引用-C#-天下网吧

新需求:

给网维制作一个修复火箭下载器工具。技术原理根据天下网吧文章:

批处理卸载/安装网吧火箭下载器服务,修复无法下载更新游戏问题

要求友好的操作界面和提示。

需求分析:

根据技术原理文章可以看到,实现这个功能很简单,一个就是启动/停止服务,一个是运行外部命令来安装/卸载火箭下载器的服务。

实现这个功能,C#相关代码资源比较多。

执行效果:

核心代码:

using System.Diagnostics;
using System.ServiceProcess;
using System.Diagnostics;
namespace FixFastGameDown
{
    public partial class Form1 : Form
    {
        String exePath, exeFile;
        /// <summary>
        /// 判断是否安装了某个服务
        /// </summary>
        /// <param name="serviceName"></param>
        /// <returns></returns>
        public static bool ISWindowsServiceInstalled(string serviceName)
        {
            try
            {
                ServiceController[] services = ServiceController.GetServices();
                foreach (ServiceController service in services)
                {
                    if (service.ServiceName == serviceName)
                    {
                        return true;
                    }
                }
                return false;
            }
            catch
            { return false; }
        }
        /// <summary>
        /// 启动某个服务
        /// </summary>
        /// <param name="serviceName"></param>
        public static void StartService(string serviceName)
        {
            try
            {
                ServiceController[] services = ServiceController.GetServices();
                foreach (ServiceController service in services)
                {
                    if (service.ServiceName == serviceName)
                    {
                        service.Start();
                        service.WaitForStatus(ServiceControllerStatus.Running, new TimeSpan(0, 0, 30));
                    }
                }
            }
            catch { }
        }
        /// <summary>
        /// 停止某个服务
        /// </summary>
        /// <param name="serviceName"></param>
        public static void StopService(string serviceName)
        {
            try
            {
                ServiceController[] services = ServiceController.GetServices();
                foreach (ServiceController service in services)
                {
                    if (service.ServiceName == serviceName)
                    {
                        service.Stop();
                        service.WaitForStatus(ServiceControllerStatus.Running, new TimeSpan(0, 0, 30));
                    }
                }
            }
            catch { }
        }
        /// <summary>
        /// 判断某个服务是否启动
        /// </summary>
        /// <param name="serviceName"></param>
        public static bool ISStart(string serviceName)
        {
            bool result = true;
            try
            {
                ServiceController[] services = ServiceController.GetServices();
                foreach (ServiceController service in services)
                {
                    if (service.ServiceName == serviceName)
                    {
                        if ((service.Status == ServiceControllerStatus.Stopped)
                            || (service.Status == ServiceControllerStatus.StopPending))
                        {
                            result = false;
                        }
                    }
                }
            }
            catch { }
            return result;
        }
        public static void RunCMDCommand(out string outContext, params String[] arguments)
        {
            int length = arguments.Length;
            outContext = "";
            if (length < 1)
                return;
            using (Process pc = new Process())
            {
                pc.StartInfo.FileName = "cmd.exe;";// 因为是执行 控制台命令 直接写死 cmd.exe
    
            pc.StartInfo.CreateNoWindow = true;
                pc.StartInfo.RedirectStandardError = true;
    
            pc.StartInfo.RedirectStandardInput = true;
                pc.StartInfo.RedirectStandardOutput = true;
                pc.StartInfo.UseShellExecute = false;
                pc.Start();

                foreach (string command in arguments)
                {
                    pc.StandardInput.WriteLine(command); //写入命令
                }
                pc.StandardInput.WriteLine("exit");  //执行结束 必要的
                pc.StandardInput.AutoFlush = true;
                outContext = pc.StandardOutput.ReadToEnd();  //读取执行的结果

                pc.WaitForExit();
                pc.Close();
            }
        }

        //执行第三方软件命令
        public static void RunExECommand(string execuable, out string outContext,String arguments)
        {
            outContext = "";
            try
            {
                Process p = Process.Start(execuable, arguments);
                p.WaitForExit();//本行代码不是必须,但是很关键,限制等待外部程序退出后才能往下执行
                
            }
            catch (Exception e)
            {

            }
        }
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            String SrvName = @"RocketSrv";
            String SrvExeFullName = @"rocketsrv.exe";
            String sTmp = "";
            //停止服务
            StopService(SrvName);
            //卸载服务 service
            RunExECommand(SrvExeFullName, out sTmp, "-UnregServer");
            //暂停5秒,等待卸载完成
            //Thread.Sleep(5000);
            //安装服务
            RunExECommand(SrvExeFullName, out sTmp, "-RegServer");
            RunExECommand(SrvExeFullName, out sTmp, "-Service");
            //启动服务
            StartService(SrvName);
            MessageBox.Show(@"修复成功!请重启火箭下载器!");
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            exePath = Application.StartupPath;
            exeFile = Application.ExecutablePath;
            System.IO.Directory.SetCurrentDirectory(AppDomain.CurrentDomain.BaseDirectory);
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Process.Start(@"https://www.txwb.com");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            String SrvName = @"RocketSrv";
            String SrvExeFullName = @"rocketsrv.exe";
            String sTmp = "";
            //停止服务
            StopService(SrvName);
            //卸载服务 service
            RunExECommand(SrvExeFullName, out sTmp, "-UnregServer");
        }

        private void button3_Click(object sender, EventArgs e)
        {
            String SrvName = @"RocketSrv";
            String SrvExeFullName = @"rocketsrv.exe";
            String sTmp = "";
            //安装服务
            RunExECommand(SrvExeFullName, out sTmp, "-RegServer");
            RunExECommand(SrvExeFullName, out sTmp, "-Service");
            StartService(SrvName);
        }
    }
}

相关资源下载:

C#修复网吧火箭下载器源码,高大上网维必备的C#开发源码,操作系统服务等源码-前端开发

本文来源:天下网吧 作者:天下码农

声明
声明:本站所发表的文章、评论及图片仅代表作者本人观点,与本站立场无关。若文章侵犯了您的相关权益,请及时与我们联系,我们会及时处理,感谢您对本站的支持!联系Email:support@txwb.com,系统开号,技术支持,服务联系QQ:1175525021本站所有有注明来源为天下网吧或天下网吧论坛的原创作品,各位转载时请注明来源链接!
天下网吧·网吧天下
  • 本周热门
  • 本月热门
  • 阅读排行