Created
January 31, 2023 14:37
-
-
Save DartPower/36cb32b93786536d8fc1678d97e09fa8 to your computer and use it in GitHub Desktop.
NetworkRestarter by ChatGPT
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Net; | |
using System.Net.NetworkInformation; | |
using System.IO; | |
namespace NetworkChecker | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
//Read the network interface name from the config file | |
string interfaceName; | |
using (StreamReader sr = new StreamReader("networkConfig.txt")) | |
{ | |
interfaceName = sr.ReadLine(); | |
} | |
//Check if the network connection is working | |
bool connectionIsWorking = false; | |
try | |
{ | |
Ping pingSender = new Ping(); | |
PingReply reply = pingSender.Send("www.google.com"); | |
if (reply.Status == IPStatus.Success) | |
{ | |
connectionIsWorking = true; | |
} | |
} | |
catch (PingException) | |
{ | |
Console.WriteLine("Ping request could not find host."); | |
} | |
if (connectionIsWorking == false) | |
{ | |
//Restart the network interface | |
NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces(); | |
foreach (NetworkInterface adapter in adapters) | |
{ | |
if (adapter.Name == interfaceName) | |
{ | |
adapter.Disable(); | |
adapter.Enable(); | |
} | |
} | |
} | |
Console.WriteLine("Done."); | |
Console.ReadLine(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment