• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

C#操作进程(Process)

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.RegularExpressions;
using System.Diagnostics;
namespace SeedServices
{
    public static class PingServicecs
    {
        private const int TIME_OUT = 100;
        private const int PACKET_SIZE = 512;
        private const int TRY_TIMES = 2;
        //检查时间的正则
        private static Regex _reg = new Regex(@"时间=(.*?)ms", RegexOptions.Multiline | RegexOptions.IgnoreCase);
        /// <summary>
        /// 结果集
        /// </summary>
        /// <param name="stringcommandLine">字符命令行</param>
        /// <param name="packagesize">丢包大小</param>
        /// <returns></returns>
        public static string LauchPing(string stringcommandLine,int packagesize)
        {
            Process process = new Process();
            process.StartInfo.Arguments = stringcommandLine;
            process.StartInfo.UseShellExecute = false;
            process.StartInfo.CreateNoWindow = true;
            process.StartInfo.FileName = "ping.exe";
            process.StartInfo.RedirectStandardInput = true;
            process.StartInfo.RedirectStandardOutput = true;
            process.StartInfo.RedirectStandardError = true;
            process.Start();
            return process.StandardOutput.ReadToEnd();//返回结果
            
        }
        /// <summary>
        /// 转换字节
        /// </summary>
        /// <param name="strBuffer">缓冲字符</param>
        /// <param name="packetSize">丢包大小</param>
        /// <returns></returns>
        private static float ParseResult(string strBuffer,int packetSize)
        {
            if (strBuffer.Length < 1)
                return 0.0F;
            MatchCollection mc = _reg.Matches(strBuffer);
            if (mc == null || mc.Count < 1 || mc[0].Groups == null)
                return 0.0F;
            if (!int.TryParse(mc[0].Groups[1].Value, out int avg))
                return 0.0F;
            if (avg <= 0)
                return 1024.0F;
            return (float)packetSize / avg * 1000 / 1024;
        }
        /// <summary>
        /// 通过Ip或网址检测调用Ping 返回 速度
        /// </summary>,
        /// <param name="strHost"></param>
        /// <returns></returns>
        public static string Test(string strHost,int trytimes,int PacketSize,int TimeOut)
        {
            return LauchPing(string.Format("{0} -n {1} -l {2} -w {3}", strHost, trytimes, PacketSize, TimeOut), PacketSize);
        }
        /// <summary>
        /// 地址
        /// </summary>
        /// <param name="strHost"></param>
        /// <returns></returns>
        public static string Test(string strHost)
        {
            return LauchPing(string.Format("{0} -n {1} -l {2} -w {3}", strHost, TRY_TIMES, PACKET_SIZE, TIME_OUT), PACKET_SIZE);
        }
    }
}

  


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C#启动外部程序的几种方法发布时间:2022-07-10
下一篇:
C# 编码规范和编程好习惯发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap