Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
- Add parameters in app.config.
- Change how to check network availability.
  • Loading branch information
And96 authored Oct 4, 2020
1 parent af79ee5 commit 664e15c
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 13 deletions.
12 changes: 9 additions & 3 deletions KeepWifiConnected/App.config
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
</startup>
<appSettings>
<add key="TimeRecheck" value="10000" />
<add key="TimeoutRequest" value="2000" />
<add key="FileLogName" value="KeepWifiConnected.log" />
<add key="UrlRequest" value="http://google.com/generate_204" />
</appSettings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
</startup>
</configuration>
4 changes: 4 additions & 0 deletions KeepWifiConnected/KeepWifiConnected.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,12 @@
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup>
<NoWin32Manifest>true</NoWin32Manifest>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Configuration" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
Expand Down
46 changes: 36 additions & 10 deletions KeepWifiConnected/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,26 @@
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Configuration;

namespace KeepWifiConnected
{
class Program
{

const int TimeRecheck = 10;
const string fileLogName = @"KeepWifiConnected.log";
const string urlRequest = @"http://google.com/generate_204";
static int timeRecheck = 0;
static int timeoutRequest = 0;
static string fileLogName = @"";
static string urlRequest = @"";

static void Main()
{
if (!SetConfig())
{
Log("Errors in .config");
Thread.Sleep(10000);
return;
}
Log("Start");
while (true)
{
Expand All @@ -41,7 +49,7 @@ static void Main()
{
Log("No Wifi Network");
}
Thread.Sleep(TimeRecheck * 1000);
Thread.Sleep(timeRecheck);
}

}
Expand Down Expand Up @@ -74,8 +82,9 @@ static bool IsInternetOn()
{
try
{
using (var client = new WebClient())
using (client.OpenRead(urlRequest))
WebRequest myWebRequest = WebRequest.Create(urlRequest);
myWebRequest.Timeout = timeoutRequest;
using (WebResponse myWebResponse = myWebRequest.GetResponse())
{
return true;
}
Expand Down Expand Up @@ -126,7 +135,7 @@ static void WifiReconnect()
WifiDisconnect();
WifiConnect(wifiNetworkName);
}

}
catch { }
}
Expand All @@ -135,14 +144,31 @@ static void Log(string message)
{
try
{
string fileLogPath = Path.Combine(Directory.GetCurrentDirectory(), fileLogName);
string content = DateTime.Now + ": " + message;
Console.WriteLine(content);
File.AppendAllText(fileLogPath, content);
File.AppendAllText(fileLogPath, Environment.NewLine);
if (fileLogName.Trim() != "")
{
string fileLogPath = Path.Combine(Directory.GetCurrentDirectory(), fileLogName);
File.AppendAllText(fileLogPath, content);
File.AppendAllText(fileLogPath, Environment.NewLine);

}
}
catch { }
}

static bool SetConfig()
{
timeRecheck = Convert.ToInt32(ConfigurationManager.AppSettings["TimeRecheck"]);
timeoutRequest = Convert.ToInt32(ConfigurationManager.AppSettings["TimeoutRequest"]);
fileLogName = ConfigurationManager.AppSettings["FileLogName"];
urlRequest = ConfigurationManager.AppSettings["UrlRequest"];
if (timeRecheck == 0 || timeoutRequest == 0 || urlRequest == "" || urlRequest == null)
{
return false;
}
return true;
}

}
}

0 comments on commit 664e15c

Please sign in to comment.