Table of Contents

Class WindowsUtils

Namespace
NanoByte.Common.Native
Assembly
NanoByte.Common.dll

Provides helper methods and API calls specific to the Windows platform.

public static class WindowsUtils
Inheritance
WindowsUtils

Fields

NetFx20

The directory version number for .NET Framework 2.0. This release includes the C# 2.0 compiler and the CLR 2.0 runtime.

public const string NetFx20 = "v2.0.50727"

Field Value

string

NetFx30

The directory version number for .NET Framework 3.0.

public const string NetFx30 = "v3.0"

Field Value

string

NetFx35

The directory version number for .NET Framework 3.5. This release includes the C# 3.0 compiler.

public const string NetFx35 = "v3.5"

Field Value

string

NetFx40

The directory version number for .NET Framework 4.x. This release includes a C# 4.0+ compiler and the CLR 4.0 runtime.

public const string NetFx40 = "v4.0.30319"

Field Value

string

Properties

AbsoluteTime

A time index in seconds that continuously increases.

public static double AbsoluteTime { get; }

Property Value

double

Remarks

Depending on the operating system this may be the time of the system clock or the time since the system booted.

CurrentProcessPath

Determines the path of the executable the current process was launched from.

public static string CurrentProcessPath { get; }

Property Value

string

HasUac

true if the current operating system supports UAC and it is enabled; false otherwise.

public static bool HasUac { get; }

Property Value

bool

IsAdministrator

Indicates whether the current user is an administrator. Always returns true on non-Windows NT systems.

public static bool IsAdministrator { get; }

Property Value

bool

IsGuiSession

Indicates whether the current process is running in a GUI session (rather than, e.g., as a service or in an SSH session).

public static bool IsGuiSession { get; }

Property Value

bool

IsWindows

true if the current operating system is Windows (9x- or NT-based); false otherwise.

public static bool IsWindows { get; }

Property Value

bool

IsWindows10

true if the current operating system is Windows 10 or newer; false otherwise.

public static bool IsWindows10 { get; }

Property Value

bool

IsWindows102004

true if the current operating system is Windows 10, Version 2004 or newer; false otherwise.

public static bool IsWindows102004 { get; }

Property Value

bool

IsWindows10Redstone

true if the current operating system is Windows 10 Anniversary Update (Redstone 1) or newer; false otherwise.

public static bool IsWindows10Redstone { get; }

Property Value

bool

IsWindows11

true if the current operating system is Windows 11 or newer; false otherwise.

public static bool IsWindows11 { get; }

Property Value

bool

IsWindows7

true if the current operating system is Windows 7 or newer; false otherwise.

public static bool IsWindows7 { get; }

Property Value

bool

IsWindows8

true if the current operating system is Windows 8 or newer; false otherwise.

public static bool IsWindows8 { get; }

Property Value

bool

IsWindowsNT

true if the current operating system is a modern Windows version (NT-based); false otherwise.

public static bool IsWindowsNT { get; }

Property Value

bool

IsWindowsVista

true if the current operating system is Windows Vista or newer; false otherwise.

public static bool IsWindowsVista { get; }

Property Value

bool

IsWindowsXP

true if the current operating system is Windows XP or newer; false otherwise.

public static bool IsWindowsXP { get; }

Property Value

bool

Methods

AttachConsole()

Tries to attach to a command-line console owned by the parent process.

public static bool AttachConsole()

Returns

bool

true if the console was successfully attached; false if the parent process did not own a console.

BroadcastMessage(int)

Sends a message of a specific type to all windows in the current session.

public static void BroadcastMessage(int messageID)

Parameters

messageID int

A unique ID number used to identify the message type session-wide.

Creates a hard link between two files.

public static void CreateHardlink(string sourcePath, string targetPath)

Parameters

sourcePath string

The path of the link to create.

targetPath string

The absolute path of the existing file to point to.

Only available on Windows 2000 or newer.

Exceptions

IOException

There was an IO problem creating the hard link.

UnauthorizedAccessException

You have insufficient rights to create the hard link.

Win32Exception

The hard link creation failed.

PlatformNotSupportedException

This method is called on a platform other than Windows NT.

Creates a symbolic link for a file or directory.

public static void CreateSymlink(string sourcePath, string targetPath)

Parameters

sourcePath string

The path of the link to create.

targetPath string

The path of the existing file or directory to point to (relative to sourcePath).

Exceptions

IOException

There was an IO problem creating the symlink.

UnauthorizedAccessException

You have insufficient rights to create the symbolic link.

Win32Exception

The symbolic link creation failed.

PlatformNotSupportedException

This method is called on a platform other than Windows NT 6.0 (Vista) or newer.

GetFileID(string)

Returns the file ID of a file.

public static long GetFileID(string path)

Parameters

path string

The path of the file.

Returns

long

Exceptions

IOException

There was an IO problem checking the file.

UnauthorizedAccessException

You have insufficient rights to check the files.

Win32Exception

Checking the file failed.

PlatformNotSupportedException

This method is called on a platform other than Windows NT.

GetFolderPath(SpecialFolder)

Gets the path to the specified system folder. Uses well-known environment variables and hard-coded paths as fallbacks when necessary.

public static string GetFolderPath(Environment.SpecialFolder folder)

Parameters

folder Environment.SpecialFolder

Returns

string

Exceptions

IOException

The folder could not be resolved to a path.

GetNetFxDirectory(string)

Returns the .NET Framework root directory for a specific version of the .NET Framework. Does not verify the directory actually exists!

public static string GetNetFxDirectory(string version)

Parameters

version string

The full .NET version number including the leading "v". Use predefined constants when possible.

Returns

string

The path to the .NET Framework root directory.

Remarks

Returns 64-bit directories if on 64-bit Windows is true.

Checks whether a file is an NTFS symbolic link.

public static bool IsSymlink(string path)

Parameters

path string

The path of the file to check.

Returns

bool

true if path points to a symbolic link; false otherwise.

Will return false for non-existing files.

Exceptions

IOException

There was an IO problem getting link information.

UnauthorizedAccessException

You have insufficient rights to get link information.

Win32Exception

Getting link information failed.

PlatformNotSupportedException

This method is called on a platform other than Windows NT 6.0 (Vista) or newer.

Checks whether a file is an NTFS symbolic link.

public static bool IsSymlink(string path, out string target)

Parameters

path string

The path of the file to check.

target string

Returns the target the symbolic link points to if it exists.

Returns

bool

true if path points to a symbolic link; false otherwise.

Exceptions

IOException

There was an IO problem getting link information.

UnauthorizedAccessException

You have insufficient rights to get link information.

Win32Exception

Getting link information failed.

PlatformNotSupportedException

This method is called on a platform other than Windows NT 6.0 (Vista) or newer.

MoveFileOnReboot(string, string?)

Moves a file on the next reboot of the OS. Replaces existing files.

public static void MoveFileOnReboot(string sourcePath, string? destinationPath)

Parameters

sourcePath string

The source path to move the file from.

destinationPath string

The destination path to move the file to. null to delete the file instead of moving it.

Remarks

Useful for replacing in-use files.

NotifyAssocChanged()

Informs the Windows shell that changes were made to the file association data in the registry.

public static void NotifyAssocChanged()

Remarks

This should be called immediately after the changes in order to trigger a refresh of the Explorer UI.

NotifyEnvironmentChanged()

Informs all GUI applications that changes where made to the environment variables (e.g. PATH) and that they should re-pull them.

public static void NotifyEnvironmentChanged()

ReadAllBytes(string)

Reads the entire contents of a file using the Win32 API.

public static byte[]? ReadAllBytes(string path)

Parameters

path string

The path of the file to read.

Returns

byte[]

The contents of the file as a byte array; null if there was a problem reading the file.

Remarks

This method works like ReadAllBytes(string), but bypasses .NET's file path validation logic.

Exceptions

PlatformNotSupportedException

This method is called on a platform other than Windows.

RegisterApplicationRestart(string)

Registers the current application for automatic restart after updates or crashes.

public static void RegisterApplicationRestart(string arguments)

Parameters

arguments string

The command-line arguments to pass to the application on restart. Must not be empty!

RegisterWindowMessage(string)

Registers a new message type that can be sent to windows.

public static int RegisterWindowMessage(string message)

Parameters

message string

A unique string used to identify the message type session-wide.

Returns

int

A unique ID number used to identify the message type session-wide.

SetCurrentProcessAppID(string)

Sets the current process' explicit application user model ID.

public static void SetCurrentProcessAppID(string appID)

Parameters

appID string

The application ID to set.

Remarks

The application ID is used to group related windows in the taskbar.

SplitArgs(string?)

Tries to split a command-line into individual arguments.

public static string[] SplitArgs(string? commandLine)

Parameters

commandLine string

The command-line to be split.

Returns

string[]

An array of individual arguments. Will return the entire command-line as one argument when not running on Windows or if splitting failed for some other reason.

UnregisterApplicationRestart()

Unregisters the current application for automatic restart after updates or crashes.

public static void UnregisterApplicationRestart()

WriteAllBytes(string, byte[])

Writes the entire contents of a byte array to a file using the Win32 API. Existing files with the same name are overwritten.

public static void WriteAllBytes(string path, byte[] data)

Parameters

path string

The path of the file to write to.

data byte[]

The data to write to the file.

Remarks

This method works like WriteAllBytes(string, byte[]), but bypasses .NET's file path validation logic.

Exceptions

IOException

There was an IO problem writing the file.

UnauthorizedAccessException

Write access to the file was denied.

Win32Exception

There was a problem writing the file.

PlatformNotSupportedException

This method is called on a platform other than Windows.