Write a C# program that checks if connected to network by pinging a specific web
ID: 3914277 • Letter: W
Question
Write a C# program that checks if connected to network by pinging a specific website (google.com), if the server replies that means it's connected to the internet and if it says "request timed out" or isn't pinging that means it is not connected to the internet. In the case of not connected to the internet, the program should close an external program, for example "utorrent" and reopen it in 1 min and then check for internet again. In the case of it is connected to internet, it should keep "utorrent" open and keep on doing this forever(while loop maybe).
Explanation / Answer
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostic;
using System.Net.NetworkInformation;
public class Program{
static bool check_connection(){
try{
Ping myping = new Ping();
String host = "8.8.8.8";
byte [] buffer = new byte[32];
int timeout = 1000;
PingOptions pingOptions = new PingOption();
PingReply reply = myping.Send(host, timeout, buffer, pingOptions);
if(reply.Status == IPStatus.Success)
return True;
else
return False;
}
catch(Exception)
return False;
}
private static Process getprocess(String processname){
Process[] aproc = Process.GetProcessByName(processname);
if(aproc.Length > 0)
return aProc[0];
else
return null;
}
static void main(String[] args){
String processname = "Change this line to exact path of the process you want to work with";
while(True){
Process myptc = Call getprocess(processname);
if(!check_connection()){
if(myptc != null)
myptc.Kill();
}
System.Thread.Sleep(60000);
}
}
}
You have to change the processname variable in main function to exact path of the process you want program to check for.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.