/// <summary>
 /// 卸载服务(关闭、删除,一条龙服务)
 /// </summary>
 /// <param name="name"></param>
 /// <param name="account">运行账户信息</param>
 public static bool Uninstall(string name, WindowsAccountModel account = null)
 {
     try
     {
         List <string> stopResult   = CMDProcessTool.Execute($"sc stop {name}", account);
         List <string> deleteResult = CMDProcessTool.Execute($"sc delete {name}", account);
         List <string> queryResult  = CMDProcessTool.Execute($"sc query {name}");
         if (Ls.Ok(queryResult))
         {
             return(queryResult.Any(x => x.Contains("失败") || x.Contains("1060") || x.Contains("服务未安装")));
         }
         return(false);
     }
     catch { return(false); }
 }
 /// <summary>
 /// 安装服务(安装、运行、设置自启,一条龙服务)
 /// </summary>
 /// <param name="name">服务名</param>
 /// <param name="path">应用程序路径</param>
 /// <param name="account">运行账户信息</param>
 /// <returns></returns>
 public static bool Install(string name, string path, WindowsAccountModel account = null)
 {
     try
     {
         List <string> createResult = CMDProcessTool.Execute($"sc create {name} binPath= \"{path}\"", account);
         List <string> startResult  = CMDProcessTool.Execute($"net start {name}", account);
         List <string> configResult = CMDProcessTool.Execute($"sc config {name} start= AUTO", account);
         if (Ls.Ok(createResult))
         {
             return(createResult.Any(x => x.Contains("成功") || x.Contains("服务已存在")));
         }
         return(false);
     }
     catch { return(false); }
 }
Example #3
0
        /// <summary>
        /// 查询列表(item1:端口、item2:pid)
        /// </summary>
        /// <param name="content">查询内容</param>
        /// <returns></returns>
        public static List <Tuple <int, int> > Find(string content)
        {
            List <Tuple <int, int> > result = null;
            var list = CMDProcessTool.Execute($"netstat -ano|findstr \"{content}\"");

            if (ListTool.HasElements(list))
            {
                result = new List <Tuple <int, int> >();
                foreach (var item in list)
                {
                    if (!string.IsNullOrWhiteSpace(item) &&
                        (item.StartsWith("TCP") || item.StartsWith("UDP")))
                    {
                        try
                        {
                            Regex    regex = new Regex(@"\s+");
                            string[] block = regex.Split(item);
                            if (ListTool.HasElements(block) && block.Length >= 3)
                            {
                                string[] s = block[1].Split(':');
                                if (ListTool.HasElements(s) && s.Length >= 2)
                                {
                                    int _port = int.Parse(s[s.Length - 1]);
                                    int _pid  = int.Parse(block[block.Length - 1]);
                                    if (!result.Any(x => x.Item1 == _port && x.Item2 == _pid))
                                    {
                                        Tuple <int, int> _tuple = new Tuple <int, int>(_port, _pid);
                                        result.Add(_tuple);
                                    }
                                }
                            }
                        }
                        catch { }
                    }
                }
            }
            return(result);
        }