[C#] C#修复网络连接代码 →→→→→进入此内容的聊天室

来自 , 2019-04-07, 写在 C#, 查看 139 次.
URL http://www.code666.cn/view/7369f3c8
  1. namespace 修复网络连接
  2. {
  3.   public partial class form1 : Form
  4.   {
  5.   DateTime ds;
  6.   int timescan;
  7.   System.Diagnostics.Process p=new System.Diagnostics.Process();  
  8.   public form1()
  9.   {
  10.   InitializeComponent();
  11.    
  12.   }
  13.  
  14.   private void renew_Click(object sender, EventArgs e)
  15.   {
  16.  
  17.   initial_timer();  
  18.  
  19.   }
  20.  
  21.   public void rebuild_con()
  22.   {
  23.   //p = new System.Diagnostics.Process();
  24.  
  25.   p.StartInfo.FileName = "cmd.exe";
  26.   //p.StartInfo.FileName = "f:\\test.bat";
  27.  
  28.   p.StartInfo.UseShellExecute = false;
  29.   p.StartInfo.RedirectStandardInput = true;
  30.   p.StartInfo.RedirectStandardOutput = true;
  31.   p.StartInfo.CreateNoWindow = true;
  32.   p.Start();
  33.   p.StandardInput.WriteLine("ipconfig /release");
  34.   p.StandardInput.WriteLine("ipconfig /renew");
  35.   p.StandardInput.WriteLine("arp -d*");
  36.   p.StandardInput.WriteLine("nbtstat -r");
  37.   p.StandardInput.WriteLine("nbtstat -rr");
  38.   p.StandardInput.WriteLine("ipconfig/ flushdns");
  39.   //MessageBox.Show(p.StandardOutput.ReadToEnd().ToString());
  40.  
  41.   }
  42.   public string NetStatus()
  43.   {
  44.   ManagementObjectCollection objects;
  45.   string status = "";
  46.   ManagementObjectSearcher searcher = new ManagementObjectSearcher();
  47.   searcher.Query.QueryString = "Select * From Win32_NetworkAdapter ";
  48.   objects = searcher.Get();
  49.   foreach (ManagementObject obj in objects)
  50.   {
  51.   foreach (PropertyData p in obj.Properties)
  52.   {
  53.   if (p.Name.Equals("NetConnectionStatus"))
  54.   {
  55.   if (p.Value != null)
  56.   {
  57.   status = p.Value.ToString();
  58.   }
  59.   }
  60.   }
  61.   }
  62.   switch (status)
  63.   {
  64.   case "0":
  65.   return "Disconnected";
  66.   case "1":
  67.   return "Connecting ...";
  68.   case "2":
  69.   return "Connected";
  70.   case "3":
  71.   return "Disconnecting ...";
  72.   case "4":
  73.   return "Hardware not present";
  74.   case "5":
  75.   return "Hardware disabled";
  76.   case "6":
  77.   return "Hardware malfunction";
  78.   case "7":
  79.   return "Media disconnected";
  80.   case "8":
  81.   return "Authenticating";
  82.   case "9":
  83.   return "Authentication succeeded";
  84.   case "10":
  85.   return "Authentication failed";
  86.   default:
  87.   return "";
  88.   }
  89.    
  90.   }
  91.  
  92.    
  93.  
  94.   private void timer1_Tick(object sender, EventArgs e)
  95.   {
  96.   rebuild_con();
  97.    
  98.    
  99.   richTextBox1.Text += "第" + (++timescan).ToString() + "次修复......";
  100.   if (timescan % 5 == 0)
  101.   {
  102.   richTextBox1.Text += "\n";
  103.   }
  104.    
  105.   string sta = NetStatus();
  106.   while (sta == "Connected")
  107.   {
  108.  
  109.   stp_exe();
  110.   richTextBox1.Text += " \n修复成功!"+"\n已停止修复!";
  111.   break;
  112.  
  113.   }
  114.   }
  115.  
  116.   private void button1_Click(object sender, EventArgs e)
  117.   {
  118.   stp_exe();
  119.   }
  120.  
  121.   private void timer2_Tick(object sender, EventArgs e)
  122.   {
  123.   int xv = progressBar1.Value == progressBar1.Maximum ? progressBar1.Value = 0 : progressBar1.Value++;
  124.   }
  125.   public void stp_exe()
  126.   {
  127.   timer1.Stop();
  128.   timer2.Stop();
  129.   progressBar1.Visible = false;
  130.   label2.Visible = false;
  131.   }
  132.   public void initial_timer()
  133.   {
  134.    
  135.   try
  136.   {
  137.   int j = int.Parse(textBox1.Text) * 1000;
  138.   if (j < 5000)
  139.   {
  140.   MessageBox.Show("时间间隔过短,数值不得小于5秒!");
  141.   return;
  142.   }
  143.   timer1.Interval = j;
  144.   }
  145.   catch (Exception ex)
  146.   {
  147.   MessageBox.Show("请输入数字类型字符!" + ex.ToString().Remove(60) + "......");
  148.   return;
  149.   }
  150.    
  151.   timer2.Interval = 500;
  152.   timer1.Start();
  153.   timer2.Start();
  154.   progressBar1.Minimum = 0;
  155.   progressBar1.Maximum = 20;
  156.   progressBar1.Visible = true;
  157.   label2.Visible = true;
  158.   }
  159.   }
  160. }
  161. //csharp/822

回复 "C#修复网络连接代码"

这儿你可以回复上面这条便签

captcha