C#修复网络连接代码

清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
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;
  }
  }
}