Skip to content

Commit

Permalink
WOL-Lite
Browse files Browse the repository at this point in the history
  • Loading branch information
PAUL GUERRE committed Jan 9, 2021
0 parents commit 7256b7a
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# WOL-Lite
Simple Wake On Lan software
![alt text](https://github.com/Paulobergine/WOL-Lite/blob/master/Release/wollite.png)
19 changes: 19 additions & 0 deletions WOL-Lite/WOL Lite.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<Window x:Class="Wake_On_Lan_Lite.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Wake_On_Lan_Lite"
mc:Ignorable="d"
Title="Wake On Lan Lite" Height="223.161" Width="419.891"
Icon="res.png">
<Grid>
<Button x:Name="WOL" Content="Wake Up !" HorizontalAlignment="Left" Margin="209,55,0,0" VerticalAlignment="Top" Width="125" Click="WOL_Click" Background="#FFE6E6E6"/>
<ListBox x:Name="LB1" HorizontalAlignment="Left" Height="100" Margin="32,23,0,0" VerticalAlignment="Top" Width="120"/>
<TextBox x:Name="TB1" HorizontalAlignment="Left" Height="23" Margin="32,128,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="120"/>
<Button x:Name="add" Content="Add" HorizontalAlignment="Left" Margin="32,156,0,0" VerticalAlignment="Top" Width="59" Click="add_Click" Background="#FF64DC41"/>
<Button x:Name="del" Content="Delete" HorizontalAlignment="Left" Margin="96,156,0,0" VerticalAlignment="Top" Width="56" Click="del_Click" Background="#FFFB0000"/>
<ProgressBar x:Name="ProgressBar1" HorizontalAlignment="Left" Height="26" Margin="166,128,0,0" VerticalAlignment="Top" Width="220" Value="0" IsIndeterminate="True"/>

</Grid>
</Window>
136 changes: 136 additions & 0 deletions WOL-Lite/WOL Lite.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
using System.Windows;
using System.Net.Sockets;
using System.Net;
using System.Globalization;
using System.Text.RegularExpressions;
using System.IO;
using System.Collections.Generic;

namespace Wake_On_Lan_Lite
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();

//Création du fichier MAC.txt pour stocker les adresses MAC
string path = @"C:\WOL";
string pathFile = @"C:\WOL\MAC.txt";

if (Directory.Exists(path))
{
if (!File.Exists(pathFile))
{
StreamWriter newFile = new StreamWriter(@"C:\WOL\MAC.txt");
newFile.Close();
}
}
else
{
DirectoryInfo di = Directory.CreateDirectory(path);
StreamWriter newFile = new StreamWriter(@"C:\WOL\MAC.txt");
newFile.Close();
}

//Récupération des adresses MAC stockées dans le fichier .txt et affichage de ces données dans la listBox
int cpt = 0;
string line;

StreamReader file = new StreamReader(@"C:\WOL\MAC.txt");
while ((line = file.ReadLine()) != null)
{
LB1.Items.Add(line);
cpt++;
}
}

private void WOL_Click(object sender, RoutedEventArgs e)
{
//Réinitialisation de la progressBar, puis appel de la méthode WakeUp (Wake On Lan)
ProgressBar1.Value = 0;
ProgressBar1.IsIndeterminate = false;

if(LB1.SelectedItem != null)
{
WakeUp(LB1.SelectedItem.ToString());
}
}

private void WakeUp(string MAC_ADDRESS)
{
//Envoie du "magic packet" vers l'adresse MAC séléctionnée dans la listBox
MAC_ADDRESS = Regex.Replace(MAC_ADDRESS, "[-|:]", "");

var sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)
{
EnableBroadcast = true
};

int payloadIndex = 0;

byte[] payload = new byte[1024];

for (int i = 0; i < 6; i++)
{
payload[payloadIndex] = 255;
payloadIndex++;
}

for (int j = 0; j < 16; j++)
{
for (int k = 0; k < MAC_ADDRESS.Length; k += 2)
{
var s = MAC_ADDRESS.Substring(k, 2);
payload[payloadIndex] = byte.Parse(s, NumberStyles.HexNumber);
payloadIndex++;
}
}

sock.SendTo(payload, new IPEndPoint(IPAddress.Parse("255.255.255.255"), 0));
sock.Close(10000);
ProgressBar1.Value = 100;
}

private void add_Click(object sender, RoutedEventArgs e)
{
//Vérification du texte entré dans la textBox (adresse MAC) et ajout de ce dernier dans la listBox ainsi que dans le fichier .txt
Regex r = new Regex("^([0-9A-Fa-f]{2}[:]){5}([0-9A-Fa-f]{2})$");

if (r.IsMatch(TB1.Text))
{
LB1.Items.Add(TB1.Text);

using (StreamWriter file =
new StreamWriter(@"C:\WOL\MAC.txt", true))
{
file.WriteLine(TB1.Text);
}
}
}

private void del_Click(object sender, RoutedEventArgs e)
{
//Suppression des adresses MAC contenues dans la listBox ainsi que dans le fichier .txt
if (LB1.Items.Count == 1)
{
LB1.Items.Clear();
string[] lines = { };
File.WriteAllLines(@"C:\WOL\MAC.txt", lines);
}
else
{
int pos = LB1.SelectedIndex;

var file = new List<string>(File.ReadAllLines(@"C:\WOL\MAC.txt"));
if(pos != 0)
{
file.RemoveAt(pos);
File.WriteAllLines(@"C:\WOL\MAC.txt", file.ToArray());
}

LB1.Items.Remove(LB1.SelectedItem);
}
}
}
}
Binary file added WOL-Lite/wollite.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 7256b7a

Please sign in to comment.