Hello,

I am trying to find all possible operative system/router/adapter reasons what can cause internet to stop connecting.
The goal is to create code that check for all those reasons and programatically resolves those issues.

I have found 2 codes that do reconnect the internet connection if not connected.
Example 1: enables the network adapter if disabled using "\" enable"

Example 2: I don't know what this code is doing. What is that code doing and how is it possible to check for if we need to "/renew" the connection?

Using "/renew" and "/release" connects and disconnects to internet in Example 2




//Example 1: Checking the network adapter
private void button2_Click(object sender, EventArgs e)
{
    bool isconnected = System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable();
    if (isconnected == false)
    {
        EnableNetWorkAdapter("Local Area Connection");
    }
}
static void EnableNetWorkAdapter(string interfaceName)
{
    System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo("netsh", "interface set interface \"" + interfaceName + "\" enable");
    System.Diagnostics.Process p = new System.Diagnostics.Process();
    p.StartInfo = psi;
    p.Start();
}




//Example 2: What is actually renewed here and how to check if we need to "/renew" the connection?
ProcessStartInfo info = new ProcessStartInfo();
info.FileName = "ipconfig";
info.Arguments = "/renew"; // or renew/release if you want to connect/disconnect
info.WindowStyle = ProcessWindowStyle.Hidden;
Process p = Process.Start(info);
p.WaitForExit();

The 2nd one is running a cmd prompt. A command you can run is ipconfig /renew. While I don't fully know what it does I have used it before when my internet is acting up to see if I can reset things and get everything back in line. However, usually I do ipconfig /release first.

The thing is you can use just ipconfig in cmd to get back information. That might be something to look at. As you can pipe the results of a cmd prompt back to C#. Now again, what exactly the "renew" command does I don't fully know. I just know I have seen this used by IT people before when for some reason I can get a connection to say wireless but I can't actually get say onto the internet.

I just googled it, there are plenty of links about this

>> However, usually I do ipconfig /release first.

That was an interesting point. Should it be okay to run the below code or do you think you need to do any additional "wait/check" code to see that the IP has been released BEFORE renew the IP?

I beleive the renew means that it does refresh the IP address in someway but are not entirely shure either even when googling around to understand exactly what it it does. If it does anything on the router/adapter etc.

//Release
ProcessStartInfo info = new ProcessStartInfo();
info.FileName = "ipconfig";
info.Arguments = "/release "; //release
info.WindowStyle = ProcessWindowStyle.Hidden;
Process p = Process.Start(info);
p.WaitForExit();

//Renew
info = new ProcessStartInfo();
info.FileName = "ipconfig";
info.Arguments = "/renew"; //renew
info.WindowStyle = ProcessWindowStyle.Hidden;
Process p = Process.Start(info);
p.WaitForExit();

That should work fine as the WaitForExit won't let the code proceed until the process is complete and those ipconfig commands don't just start and forget, they wait for their process to finish (now they can hang awhile sometimes, can't remember what their time out is).

As for the meaning, I found a good article I believe last time, but now I can't find it. From what I believe it does is frees up data that might be cached with say a router (or some caching clearing up). Which I know stuff can get stuck, corrupted, or have issues when an item is cached (and sometimes you just need a clean slate)

it does is frees up data that might be cached with say a router.........

That is a good thing. That is what I also experience that even sometimes you need to restart the router or shutdown the power and put it on for internet to work again and have the ability to use the /renew to renew the IP might be something that could solve that programatically.

That can help, but sometimes yes you have to reset the router. I am no stranger to that either (or if you want really crazy, Games for Windows Live would actually drop my internet when it tried to connect. Had to deal with this Linksys rounter I had. Only way around it was to simply not use it)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.