| 注册
请输入搜索内容

热门搜索

Java Linux MySQL PHP JavaScript Hibernate jQuery Nginx
bdnp
10年前发布

C#修复网络连接代码

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;    }    }  }