Click here to Skip to main content
15,867,860 members
Articles / Programming Languages / C#

Wi-Fi Timer

Rate me:
Please Sign up or sign in to vote.
5.00/5 (11 votes)
25 Nov 2013CPOL 21.8K   17   4
Schedule your wifi status

Introduction

In many situations, I needed to control wi-fi status on my PC. Tuning on/off automatically [midnight > morning] well, because my ISP doesn't invoice on traffic in that interval, and some programs need/use large amount of data..

Image 1Image 2

Background

This is an easy way is to use command prompt cmd.exe.

netsh.exe does the job. [under administrator privileges]*

Enable Interface: netsh interface set interface "interfaceName" enable

Disable Interface: netsh interface set interface "interfaceName" disable

Using the Code

In brief, running this command in C#. By starting netsh.exe process with the above explained arguments:

C#
private enum Status
{
    Online,
    Offline
}
 
private static void SetStatus(string interfaceName, Status status)
{
    string state = status == Status.Online ? "enable" : "disable";
 
    string arguments = "interface set interface \"{0}\" {1}";
 
    string args = string.Format(arguments, interfaceName, state);
 
    var NetshStartInfo = new System.Diagnostics.ProcessStartInfo("netsh", args)
    {
        CreateNoWindow = true,
        RedirectStandardOutput = true,
        UseShellExecute = false,
        WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden
    };
 
    var Netsh = new System.Diagnostics.Process
    {
        StartInfo = NetshStartInfo
    };
 
    Netsh.Start();
}

The source code includes usage of the application settings to load/save options, Linq expressions, and some WPF styling...

* to run with administrator permission, add to the app.manifest file in solution explorer these line of code into:

XML
<asmv1:assembly ...>  
<.../>      
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
<security>
<requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
        <requestedExecutionLevel level="highestAvailable" uiAccess="false" />
      </requestedPrivileges>
    </security> 
</trustInfo>
</asmv1:assembly>  

Points of Interest

It's a light way. Usage is up to certain limits and conditions. Advanced solution can be by scheduling firewall rules on certain programs. And starting them with arguments, etc.

Thanks! Big Grin | <img src=

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
Lebanon Lebanon
Bitcoin, Ethereum - Smartcontracts, Full Stack, Architecture & Development, Music!

Comments and Discussions

 
BugStay where it was... Pin
Peter Leow22-Apr-14 1:45
professionalPeter Leow22-Apr-14 1:45 
GeneralRe: Stay where it was... Pin
OriginalGriff22-Apr-14 1:59
mveOriginalGriff22-Apr-14 1:59 
GeneralMy vote of 5 Pin
johannesnestler25-Nov-13 4:35
johannesnestler25-Nov-13 4:35 
GeneralRe: My vote of 5 Pin
F. Aro25-Nov-13 8:35
professionalF. Aro25-Nov-13 8:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.