C#重啟網卡(支持xp和win7)


提示:

XP使用的是:devcon.exe

win7使用的是:netsh命令

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.IO;
//using System.Drawing;
using System.Linq;
using System.Management;
using System.Net.NetworkInformation;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 自動啟用網卡
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        Thread tdNetWork=null;
        private void Form1_Load(object sender, EventArgs e)
        {
            StreamReader sr=new StreamReader("C:\\PCI.txt");
            string line;
            if((line = sr.ReadLine()) != null)
            {
                tbNetWorkName.Text = line;
            }
            sr.Close();
            GetPlatForm();//獲取操作系統平台
        }
        //獲取系統版本
        private void GetPlatForm()
        {
            OperatingSystem os = Environment.OSVersion;
            switch (os.Platform)
            {
                case PlatformID.Win32Windows:
                    switch (os.Version.Minor)
                    {
                        case 0:
                            labelOS.Text = "Windows 95";
                            break;
                        case 10:
                            if (os.Version.Revision.ToString() == "2222A ")
                                labelOS.Text = "Windows 98 第二版";
                            else
                                labelOS.Text = "Windows 98";
                            break;
                        case 90:
                            labelOS.Text = "Windows Me";
                            break;
                    }
                    break;
                case PlatformID.Win32NT:
                    switch (os.Version.Major)
                    {
                        case 3:
                            labelOS.Text = "Windows NT 3.51";
                            break;
                        case 4:
                            labelOS.Text = "Windows NT 4.0";
                            break;
                        case 5:
                            switch (os.Version.Minor)
                            {
                                case 0:
                                    labelOS.Text = "Windows 2000";
                                    break;
                                case 1:
                                    labelOS.Text = "Windows XP";
                                    break;
                                case 2:
                                    labelOS.Text = "Windows 2003";
                                    break;
                            }
                            break;
                        case 6:
                            switch (os.Version.Minor)
                            {
                                case 0:
                                    labelOS.Text = "Windows Vista";
                                    break;
                                case 1:
                                    labelOS.Text = "Windows 7";
                                    break;
                            }
                            break;
                    }
                    break;
            }
        }       

        private void btnStop_Click(object sender, EventArgs e)
        {
            if (tdNetWork.IsAlive)
            {
                tdNetWork.Abort();
            }
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            if (labelOS.Text == "Windows 7")
            {
                if (tbNetWorkName.Text == null)
                {
                    MessageBox.Show("請填寫好網卡名");
                    tbNetWorkName.Focus();
                }
            }
            else
            {
                if (tbNetWorkName.Text == null)
                {
                    MessageBox.Show("請填寫好設備ID");
                    tbNetWorkName.Focus();
                }
            }
            if (tdNetWork == null)
            {
                tdNetWork = new Thread(new ParameterizedThreadStart(startNetWork));
                tdNetWork.IsBackground = true;
                tdNetWork.Start(tbNetWorkName.Text);
            }
        }
        //啟用網卡
        private void startNetWork(object NetWorkName)
        {
            while (true)
            {
                if (labelOS.Text == "Windows 7")
                {
                    Execute(string.Format("netsh interface set interface \"{0}\" enabled", NetWorkName), 10);
                }
                else
                {
                    Execute(string.Format("devcon enable PCI\\{0}*", NetWorkName), 10);
                }
                Thread.Sleep(5000);
            }
        }

        /// <summary>  
        /// 執行DOS命令,返回DOS命令的輸出  
        /// </summary>  
        /// <param name="dosCommand">dos命令</param>  
        /// <param name="milliseconds">等待命令執行的時間(單位:毫秒),  
        /// 如果設定為0,則無限等待</param>  
        /// <returns>返回DOS命令的輸出</returns>  
        public static string Execute(string command, int seconds)
        {
            string output = ""; //輸出字符串  
            if (command != null && !command.Equals(""))
            {
                Process process = new Process();//創建進程對象  
                ProcessStartInfo startInfo = new ProcessStartInfo();
                startInfo.FileName = "cmd.exe";//設定需要執行的命令  
                startInfo.Arguments = "/C " + command;//“/C”表示執行完命令后馬上退出  
                startInfo.UseShellExecute = false;//不使用系統外殼程序啟動  
                startInfo.RedirectStandardInput = false;//不重定向輸入  
                startInfo.RedirectStandardOutput = true; //重定向輸出
                startInfo.CreateNoWindow = true;//不創建窗口
                process.StartInfo = startInfo;
                try
                {
                    if (process.Start())//開始進程  
                    {
                        if (seconds == 0)
                        {
                            process.WaitForExit();//這里無限等待進程結束  
                        }
                        else
                        {
                            process.WaitForExit(seconds); //等待進程結束,等待時間為指定的毫秒  
                        }
                        output = process.StandardOutput.ReadToEnd();//讀取進程的輸出  
                    }
                }
                catch
                {
                }
                finally
                {
                    if (process != null)
                        process.Close();
                }
            }
            return output;
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            FileStream fs = new FileStream("C:\\PCI.txt", FileMode.Create);
            //獲得字節數組
            byte[] data = new UTF8Encoding().GetBytes(tbNetWorkName.Text);
            //開始寫入
            fs.Write(data, 0, data.Length);
            //清空緩沖區、關閉流
            fs.Flush();
            fs.Close();
        }
    }
}

參考文章:http://blog.chinaunix.net/uid-14388574-id-150699.html

原創文章,轉載請注明出處:http://www.cnblogs.com/hongfei/archive/2013/04/15/3022708.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM