/stay-awake-cs

Keep a Windows machine awake

Primary LanguageC#Apache License 2.0Apache-2.0

stay-awake-cs

Keep a Windows machine awake using the win32 API.

Reason for Development

Other planned experiments include:

Explore .NET Core 3 features

Distribution

Building

Colophon

Requirements

Built with .NET Core 3.

Version information compiled using versions.ps1 script.

Windows version: 10.0.19013.0

dotnet core version: 3.1.100-preview2-014569

Package versions:
Project 'stay-awake' has the following package references
   [netcoreapp3.1]:
   Top-level Package                     Requested
 Resolved
   > System.CommandLine.DragonFruit      0.3.0-alpha.19405.1   0.3.0-alpha.19405.1

Initial Setup

Create a new project.

dotnet new console -o stay-awake

Add dependency for System.CommandLine.DragonFruit.

dotnet add package System.CommandLine.DragonFruit --version 0.3.0-alpha.19405.1

Build, Run, Test

Build

dotnet build

Run

# Help 
dotnet run -- --help

# Keep display on
dotnet run -- --awake-mode Display

Test

In order to test, open PowerShell with elevated (admin) privileges. After executing the program, run the following.

powercfg -requests

Publish

To produce a single file exe, add the following to the stay-awake.csproj file.

<PropertyGroup>
    <RuntimeIdentifier>win10-x64</RuntimeIdentifier>
    <PublishSingleFile>true</PublishSingleFile>
</PropertyGroup>

For assembly linking, add the following to the stay-awake.csproj file.

<PropertyGroup>
   <PublishTrimmed>true</PublishTrimmed>
</PropertyGroup>

For a ReadyToRun image, add the following to the stay-awake.csproj file.

<PropertyGroup>
  <PublishReadyToRun>true</PublishReadyToRun>
</PropertyGroup>

To publish, run the following.

dotnet publish -c Release

To test the exe, run the following.

.\bin\Release\netcoreapp3.1\win10-x64\stay-awake.exe --help

Finally, to check the size of the exe, run the following.

# In KB
(Get-ChildItem -File .\bin\Release\netcoreapp3.1\win10-x64\stay-awake.exe).Length/1KB

# In MB
(Get-ChildItem -File .\bin\Release\netcoreapp3.1\win10-x64\stay-awake.exe).Length/1MB

Local Tool

To produce a package for use as a dotnet tool, update the csproj file by adding the following.

<PackAsTool>true</PackAsTool>
<ToolCommandName>stay-awake</ToolCommandName>
<PackageOutputPath>./nupkg</PackageOutputPath>

Next, create a NuGet package (the configuration doesn't really matter at this point).

dotnet pack -c Release

Install as a global tool on your system.

dotnet tool install stay-awake --add-source ./ --global

Update as a global tool on your system.

dotnet tool update stay-awake --add-source ./ --global

To run, add the directory C:\Users\myusername\.dotnet\tools to your PATH. Finally, run the tool!

stay-awake.exe --help

References