C#修復網絡連接代碼

bdnp 9年前發布 | 3K 次閱讀 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; } } }</pre>

 本文由用戶 bdnp 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!