System
Collection of classes in C# helping you to execute system operations.
Content:
Installation
Install-Package FrApp42.System
Examples
Avake
- AnthoDingo - Author (taken from PowerToys)
V1
Simple usage
Program.cs
using FrApps42.System.Computer.Awake.v1;
...
// Keep Screen on
Awake..SetIndefiniteKeepAwake(true);
// Keep Screen off
Awake..SetIndefiniteKeepAwake(false);
// Disable Keep Awake
Awake.SetNoKeepAwake();
...
Awake.CompleteExit(0, false, "AppName");
Log Awake error
Program.cs
using FrApps42.System.Computer.Awake.v1;
...
private static void LogUnexpectedOrCancelledKeepAwakeThreadCompletion(){
Console.WriteLine("The keep-awake thread was terminated early.");
}
private static void LogCompletedKeepAwakeThread(bool result)
{
Console.WriteLine($"Exited keep-awake thread successfully: {result}");
}
// Keep Screen on
Awake..SetIndefiniteKeepAwake(LogCompletedKeepAwakeThread, LogUnexpectedOrCancelledKeepAwakeThreadCompletion,true);
// Keep Screen off
Awake..SetIndefiniteKeepAwake(LogCompletedKeepAwakeThread, LogUnexpectedOrCancelledKeepAwakeThreadCompletion,false);
// Disable Keep Awake
Awake.SetNoKeepAwake();
...
Awake.CompleteExit(0, false, "AppName");
V2
Updated version of Power Awake
Program.cs
using FrApps42.System.Computer.Awake.v1;
...
private static void LogUnexpectedOrCancelledKeepAwakeThreadCompletion(){
Console.WriteLine("The keep-awake thread was terminated early.");
}
private static void LogCompletedKeepAwakeThread(bool result)
{
Console.WriteLine($"Exited keep-awake thread successfully: {result}");
}
// Keep Screen on
Awake..SetIndefiniteKeepAwake(true);
// Keep Screen off
Awake..SetIndefiniteKeepAwake(false);
// Keep Awake for a specified seconds with screen on
Awake.SetTimedKeepAwake(3600, true);
// Keep Awake for a specified seconds with screen off
Awake.SetTimedKeepAwake(3600, false);
// Disable Keep Awake
Awake.SetNoKeepAwake();
In V2, be sure to disable KeepAwake before app closing.
IsOnline
- AnthoDingo - Author
Check if device is online
Program.cs
using FrApp42.System.Net;
string address = "your-address";
string timeout = 5;
IsOnline online = new(address, timeout);
bool isOnline = online.Check();
Shutdown (Windows-only)
- Sikelio - Author
- AnthoDingo - Contributor
Shutdown a specific computer
Program.cs
using FrApp42.System.Computer.Shutdown;
string hostname = "your-hostame";
Shutdown shutdown = new();
shutdown
.SetMachine(hostname)
.ShutdownComputer();
ShutdownResult result = await shutdown.Run();
if (result.ExitCode == 0)
{
Console.WriteLine("Shutdown command executed successfully.");
}
else
{
Console.WriteLine($"Shutdown command failed with exit code {result.ExitCode}. Error: {result.ErrorMessage}");
}
Ping before shutdown
using FrApp42.System.Computer.Shutdown;
string hostname = "your-hostame";
Shutdown shutdown = new();
shutdown
.SetMachine(hostname)
.PingBefore(5);
ShutdownResult result = await shutdown.Run();
if (result.ExitCode == 0)
{
Console.WriteLine("Shutdown command executed successfully.");
}
else
{
Console.WriteLine($"Shutdown command failed with exit code {result.ExitCode}. Error: {result.ErrorMessage}");
}
Log off the current user
Program.cs
using FrApp42.System.Computer.Shutdown;
Shutdown shutdown = new();
shutdown
.LogoutUser();
ShutdownResult result = await shutdown.Run();
if (result.ExitCode == 0)
{
Console.WriteLine("Shutdown command executed successfully.");
}
else
{
Console.WriteLine($"Shutdown command failed with exit code {result.ExitCode}. Error: {result.ErrorMessage}");
}
Shutdown and sign on automatically
Program.cs
using FrApp42.System.Computer.Shutdown;
Shutdown shutdown = new();
shutdown
.ShutdownAndSignOn();
ShutdownResult result = await shutdown.Run();
if (result.ExitCode == 0)
{
Console.WriteLine("Shutdown command executed successfully.");
}
else
{
Console.WriteLine($"Shutdown command failed with exit code {result.ExitCode}. Error: {result.ErrorMessage}");
}
Set a timeout and add a custom reason for the shutdown
Program.cs
using FrApp42.System.Computer.Shutdown;
Shutdown shutdown = new();
shutdown
.SetTimeOut(60)
.SetComment("Scheduled maintenance");
ShutdownResult result = await shutdown.Run();
if (result.ExitCode == 0)
{
Console.WriteLine("Shutdown command executed successfully.");
}
else
{
Console.WriteLine($"Shutdown command failed with exit code {result.ExitCode}. Error: {result.ErrorMessage}");
}
WakeOnLan
Program.cs
using FrApp42.System.Net;
string macAddress = "your-mac-address";
await WakeOnLan.WakeUp(macAddress);