-
Notifications
You must be signed in to change notification settings - Fork 0
Console App
One of the most significant breakthroughs in ElectronNET.Core is the ability to build Electron applications using simple console applications instead of requiring ASP.NET Core. This removes a major barrier and enables many more use cases.
Console applications with ElectronNET.Core support multiple content scenarios:
- File System HTML/JS - Serve static web content directly from the file system
- Remote Server Integration - Connect to existing web servers or APIs
- Lightweight Architecture - Avoid ASP.NET overhead when not needed
- Simplified Deployment - Package and distribute with minimal dependencies
Before starting, ensure you have:
- .NET 8.0 or later
- Node.js 22.x or later
- Visual Studio 2022 (recommended) or Visual Studio Code
Create a new Console Application project in Visual Studio:
dotnet new console -n MyElectronApp
cd MyElectronAppPM> Install-Package ElectronNET.CoreNote: The API package is automatically included as a dependency of
ElectronNET.Core.
Add the Electron.NET configuration to your .csproj file:
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="ElectronNET.Core" Version="1.0.0" />
</ItemGroup>Here's a complete console application example:
using System;
using System.Threading.Tasks;
using ElectronNET.API.Entities;
namespace MyElectronApp
{
public class Program
{
public static async Task Main(string[] args)
{
var runtimeController = ElectronNetRuntime.RuntimeController;
try
{
// Start Electron runtime
await runtimeController.Start();
await runtimeController.WaitReadyTask;
// Initialize your Electron app
await InitializeApp();
// Wait for shutdown
await runtimeController.WaitStoppedTask;
}
catch (Exception ex)
{
Console.WriteLine($"Error: {ex.Message}");
await runtimeController.Stop();
await runtimeController.WaitStoppedTask.WaitAsync(TimeSpan.FromSeconds(2));
}
}
private static async Task InitializeApp()
{
// Create main window
var browserWindow = await Electron.WindowManager.CreateWindowAsync(
new BrowserWindowOptions
{
Width = 1200,
Height = 800,
Show = false,
WebPreferences = new WebPreferences
{
NodeIntegration = false,
ContextIsolation = true
}
});
// Load your content (file system, remote URL, etc.)
await browserWindow.WebContents.LoadURLAsync("https://example.com");
// Show window when ready
browserWindow.OnReadyToShow += () => browserWindow.Show();
}
}
}Serve HTML/JS files from your project:
// In your project root, create wwwroot/index.html
await browserWindow.WebContents.LoadFileAsync("wwwroot/index.html");Load content from any web server:
await browserWindow.WebContents.LoadURLAsync("https://your-server.com/app");For development, you can run a simple HTTP server:
// Add this for development
if (Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") == "Development")
{
await browserWindow.WebContents.LoadURLAsync("http://localhost:3000");
}Configure Electron settings through MSBuild properties in your .csproj:
<PropertyGroup>
<ElectronNETCoreDescription>My Electron App</ElectronNETCoreDescription>
<ElectronNETCoreDisplayName>MyApp</ElectronNETCoreDisplayName>
<ElectronNETCoreAuthorName>Your Name</ElectronNETCoreAuthorName>
</PropertyGroup>Access configuration at runtime:
var app = await Electron.App.GetAppAsync();
Console.WriteLine($"App Name: {app.Name}");Customize your main window:
var options = new BrowserWindowOptions
{
Width = 1400,
Height = 900,
MinWidth = 800,
MinHeight = 600,
Frame = true,
Title = "My Custom App",
Icon = "assets/app-icon.png"
};Create additional windows as needed:
var settingsWindow = await Electron.WindowManager.CreateWindowAsync(
new BrowserWindowOptions { Width = 600, Height = 400, Modal = true },
"app://settings.html");- Debugging - Learn about debugging console applications
- Package Building - Create distributable packages
- Migration Guide - Moving from ASP.NET projects
✅ Simpler Architecture - No ASP.NET complexity when not needed ✅ Flexible Content - Use any HTML/JS source ✅ Faster Development - Less overhead for simple applications ✅ Easy Deployment - Minimal dependencies ✅ Better Performance - Lighter weight than full web applications
Want to contribute to this documentation? Please fork and create a PR! The Wiki is autogenerated from the /docs content in the repository.