.NET library for Windows multiple desktop operations
Platform | NuGet | Downloads |
---|---|---|
Core | ||
Forms | ||
WPF |
- Switch, add, and remove a virtual desktop
- Move any window to any virtual desktop
- Pin any window or application for them to show on all desktops
- Notifications when switching, deleting, or renaming virtual desktops
- Change the wallpaper for each desktop
samples/VirtualDesktop.Showcase
<TargetFramework>net6.0-windows10.0.19041.0</TargetFramework>
- .NET 6.0
- .NET 7.0
- .NET 8.0
- Windows 10 build 19041 (20H1) or later
Install NuGet package(s).
PM> Install-Package VirtualDesktop
- VirtualDesktop - Core classes for VirtualDesktop.
- VirtualDesktop.WPF - Provides extension methods for WPF Window class.
- VirtualDesktop.WinForms - Provides extension methods for Form class.
Because of the dependency on C#/WinRT, the target framework must be set to net6.0-windows10.0.19041.0
or later.
<TargetFramework>net6.0-windows10.0.19041.0</TargetFramework>
If it does not work, try creating an app.manifest
file optimized to work on Windows 10.
<compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
<application>
<!-- Windows 10 / 11-->
<supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}" />
</application>
</compatibility>
The namespace to use is WindowsDesktop
.
using WindowsDesktop;
// Get all virtual desktops
var desktops = VirtualDesktop.GetDesktops();
// Get Virtual Desktop for specific window
var desktop = VirtualDesktop.FromHwnd(hwnd);
// Get the left/right desktop
var left = desktop.GetLeft();
var right = desktop.GetRight();
// Create new
var desktop = VirtualDesktop.Create();
// Remove
desktop.Remove();
// Switch
desktop.GetLeft().Switch();
// Notification of desktop switching
VirtualDesktop.CurrentChanged += (_, args) => Console.WriteLine($"Switched: {args.NewDesktop.Name}");
// Notification of desktop creating
VirtualDesktop.Created += (_, desktop) => desktop.Switch();
// Need to install 'VirtualDesktop.WPF' package
// Check whether a window is on the current desktop.
var isCurrent = window.IsCurrentVirtualDesktop();
// Get Virtual Desktop for WPF window
var desktop = window.GetCurrentDesktop();
// Move window to specific Virtual Desktop
window.MoveToDesktop(desktop);
// Pin window
window.Pin()
Since this library is using undocumented interfaces, you need to reverse engineer your Windows version to support it.
The class IDs of undocumented interfaces tend to change between different OS versions. If the demo application crashes on start-up, you will need to provide the interfaces' IDs matching your Windows version.
Here are the interfaces we need:
IApplicationView
IApplicationViewCollection
IObjectArray
IServiceProvider
IVirtualDesktop
IVirtualDesktopManager
IVirtualDesktopManagerInternal
IVirtualDesktopNotification
IVirtualDesktopNotificationService
IVirtualDesktopPinnedApps
Once you have those IDs, add them in a new setting
element in app.config.
Make sure to specify the correct 9 digits Windows build and cumulative update version.
You can get it using one of those methods:
- From the UI run:
winver
- From shell run:
ver
- From powershell run:
cmd /c ver
Don't forget to contribute back your changes.
To publish a new release specify your version in Directory.Build.props and push the changes with a commit description such as:
Release vx.y.z
where x
, y
, z
form your version number. That should publish it on NuGet providing that your secret NUGET_API_KEY
is still valid.
This library is essentially a C# wrapper for IVirtualDesktopManager and related undocumented interfaces. In order to support breaking binary changes between Windows versions we perform runtime compilation of a DLL providing access to the COM interfaces matching your OS build version.
- samples/README.md
- StackOverflow
- Upstream repository - unmaintained
- VirtualDesktop command line tool - not using this library
- VirtualDesktop AutoHotKey DLL
- C#/WinRT repository
This library is under the MIT License.
- Thanks @Grabacr07 for creating this great piece of software
- All contributors for sharing your work with the community