Simulating network failure through C#

using System;

using System.Management;

using System.Collections.Generic;

using System.Linq;

 

namespace ConsoleApplication1

{

    class Program

    {

        static void Main(string[] args)

        {

            Disconnect(60000);

        }

 

        public static void Disconnect(int millisecondsTimeout)

        {

            Disable();

            Console.WriteLine("Network Disabled");

            System.Threading.Thread.Sleep(millisecondsTimeout);

            Enable();

            Console.WriteLine("Network Enabled");

        }

       

        public static void Disable()

        {

            SetIP("192.168.0.4", "255.255.255.0");

        }

 

        public static void Enable()

        {

            SetDHCP();

        }

 

        public static void SetIP(string ip_address, string subnet_mask)

        {

            ManagementClass objMC = new ManagementClass("Win32_NetworkAdapterConfiguration");

            ManagementObjectCollection objMOC = objMC.GetInstances();

 

            foreach (ManagementObject objMO in objMOC)

            {

                if ((bool)objMO["IPEnabled"])

                {

                    try

                    {

                        ManagementBaseObject setIP = default(ManagementBaseObject);

                        ManagementBaseObject newIP = objMO.GetMethodParameters("EnableStatic");

                        newIP["IPAddress"] = new string[] { ip_address };

                        newIP["SubnetMask"] = new string[] { subnet_mask };

                        setIP = objMO.InvokeMethod("EnableStatic", newIP, null);

                    }

                    catch (Exception generatedExceptionName)

                    {

                        Console.WriteLine(generatedExceptionName.Message);

                        throw;

                    }

                }

            }

        }

 

        private static void SetDHCP()

        {

            ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");

            ManagementObjectCollection moc = mc.GetInstances();

 

            foreach (ManagementObject mo in moc)

            {

                if ((bool)mo["IPEnabled"])

                {

                    ManagementBaseObject newDNS = mo.GetMethodParameters("SetDNSServerSearchOrder");

                    newDNS["DNSServerSearchOrder"] = null;

                    ManagementBaseObject enableDHCP = mo.InvokeMethod("EnableDHCP", null, null);

                    ManagementBaseObject setDNS = mo.InvokeMethod("SetDNSServerSearchOrder", newDNS, null);

                }

            }

        }

    }

}

Advertisements
This entry was posted in Information Technology. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s