加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

C#修复网络连接代码

发布时间:2020-12-15 17:54:45 所属栏目:百科 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 namespace 修复网络连接{ public partial class form1 : Form { DateTime ds; int timescan; System.Diagnostics.Process p=new System.Diagnostics.P

以下代码由PHP站长网 52php.cn收集自互联网

现在PHP站长网小编把它分享给大家,仅供参考

namespace 修复网络连接
{
  public partial class form1 : Form
  {
  DateTime ds;
  int timescan;
  System.Diagnostics.Process p=new System.Diagnostics.Process(); 
  public form1()
  {
  InitializeComponent();
     
  }
 
  private void renew_Click(object sender,EventArgs e)
  {
 
  initial_timer();  
 
  }
 
  public void rebuild_con()
  {
  //p = new System.Diagnostics.Process();
 
  p.StartInfo.FileName = "cmd.exe";
  //p.StartInfo.FileName = "f:test.bat";
 
  p.StartInfo.UseShellExecute = false;
  p.StartInfo.RedirectStandardInput = true;
  p.StartInfo.RedirectStandardOutput = true;
  p.StartInfo.CreateNoWindow = true;
  p.Start();
  p.StandardInput.WriteLine("ipconfig /release");
  p.StandardInput.WriteLine("ipconfig /renew");
  p.StandardInput.WriteLine("arp -d*");
  p.StandardInput.WriteLine("nbtstat -r");
  p.StandardInput.WriteLine("nbtstat -rr");
  p.StandardInput.WriteLine("ipconfig/ flushdns");
  //MessageBox.Show(p.StandardOutput.ReadToEnd().ToString());
 
  }
  public string NetStatus()
  {
  ManagementObjectCollection objects;
  string status = "";
  ManagementObjectSearcher searcher = new ManagementObjectSearcher();
  searcher.Query.QueryString = "Select * From Win32_NetworkAdapter ";
  objects = searcher.Get();
  foreach (ManagementObject obj in objects)
  {
  foreach (PropertyData p in obj.Properties)
  {
  if (p.Name.Equals("NetConnectionStatus"))
  {
  if (p.Value != null)
  {
  status = p.Value.ToString();
  }
  }
  }
  }
  switch (status)
  {
  case "0":
  return "Disconnected";
  case "1":
  return "Connecting ...";
  case "2":
  return "Connected";
  case "3":
  return "Disconnecting ...";
  case "4":
  return "Hardware not present";
  case "5":
  return "Hardware disabled";
  case "6":
  return "Hardware malfunction";
  case "7":
  return "Media disconnected";
  case "8":
  return "Authenticating";
  case "9":
  return "Authentication succeeded";
  case "10":
  return "Authentication failed";
  default:
  return "";
  }
     
  }
 
     
 
  private void timer1_Tick(object sender,EventArgs e)
  {
  rebuild_con();
     
     
  richTextBox1.Text += "第" + (++timescan).ToString() + "次修复......";
  if (timescan % 5 == 0)
  {
  richTextBox1.Text += "n";
  }
     
  string sta = NetStatus();
  while (sta == "Connected")
  {
 
  stp_exe();
  richTextBox1.Text += " n修复成功!"+"n已停止修复!";
  break;
 
  }
  }
 
  private void button1_Click(object sender,EventArgs e)
  {
  stp_exe();
  }
 
  private void timer2_Tick(object sender,EventArgs e)
  {
  int xv = progressBar1.Value == progressBar1.Maximum ? progressBar1.Value = 0 : progressBar1.Value++;
  }
  public void stp_exe()
  {
  timer1.Stop();
  timer2.Stop();
  progressBar1.Visible = false;
  label2.Visible = false;
  }
  public void initial_timer()
  {
     
  try
  {
  int j = int.Parse(textBox1.Text) * 1000;
  if (j < 5000)
  {
  MessageBox.Show("时间间隔过短,数值不得小于5秒!");
  return;
  }
  timer1.Interval = j;
  }
  catch (Exception ex)
  {
  MessageBox.Show("请输入数字类型字符!" + ex.ToString().Remove(60) + "......");
  return;
  }
     
  timer2.Interval = 500;
  timer1.Start();
  timer2.Start();
  progressBar1.Minimum = 0;
  progressBar1.Maximum = 20;
  progressBar1.Visible = true;
  label2.Visible = true;
  }
  }
}

以上内容由PHP站长网【52php.cn】收集整理供大家参考研究

如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读