Show / Hide Table of Contents

Class WindowsUtils

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

Inheritance
Object
WindowsUtils
Namespace: NanoByte.Common.Native
Assembly: NanoByte.Common.dll
Syntax
public static class WindowsUtils : Object

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.

Declaration
public const string NetFx20 = "v2.0.50727"
Field Value
Type Description
String

NetFx30

The directory version number for .NET Framework 3.0.

Declaration
public const string NetFx30 = "v3.0"
Field Value
Type Description
String

NetFx35

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

Declaration
public const string NetFx35 = "v3.5"
Field Value
Type Description
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.

Declaration
public const string NetFx40 = "v4.0.30319"
Field Value
Type Description
String

Properties

AbsoluteTime

A time index in seconds that continuously increases.

Declaration
public static double AbsoluteTime { get; }
Property Value
Type Description
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.

Declaration
public static string CurrentProcessPath { get; }
Property Value
Type Description
String

HasUac

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

Declaration
public static bool HasUac { get; }
Property Value
Type Description
Boolean

IsAdministrator

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

Declaration
public static bool IsAdministrator { get; }
Property Value
Type Description
Boolean

IsGuiSession

Indicates whether the current process is running in a GUI session (rather than, e.g., as a service or in an SSH session). Always false on non-Windows systems.

Declaration
public static bool IsGuiSession { get; }
Property Value
Type Description
Boolean

IsWindows

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

Declaration
public static bool IsWindows { get; }
Property Value
Type Description
Boolean

IsWindows10

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

Declaration
public static bool IsWindows10 { get; }
Property Value
Type Description
Boolean

IsWindows102004

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

Declaration
public static bool IsWindows102004 { get; }
Property Value
Type Description
Boolean

IsWindows10Redstone

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

Declaration
public static bool IsWindows10Redstone { get; }
Property Value
Type Description
Boolean

IsWindows11

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

Declaration
public static bool IsWindows11 { get; }
Property Value
Type Description
Boolean

IsWindows7

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

Declaration
public static bool IsWindows7 { get; }
Property Value
Type Description
Boolean

IsWindows8

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

Declaration
public static bool IsWindows8 { get; }
Property Value
Type Description
Boolean

IsWindowsNT

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

Declaration
public static bool IsWindowsNT { get; }
Property Value
Type Description
Boolean

IsWindowsVista

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

Declaration
public static bool IsWindowsVista { get; }
Property Value
Type Description
Boolean

IsWindowsXP

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

Declaration
public static bool IsWindowsXP { get; }
Property Value
Type Description
Boolean

Methods

AttachConsole()

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

Declaration
public static bool AttachConsole()
Returns
Type Description
Boolean

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

BroadcastMessage(Int32)

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

Declaration
public static void BroadcastMessage(int messageID)
Parameters
Type Name Description
Int32 messageID

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

CreateHardlink(String, String)

Creates a hard link between two files.

Declaration
public static void CreateHardlink(string sourcePath, string targetPath)
Parameters
Type Name Description
String sourcePath

The path of the link to create.

String targetPath

The absolute path of the existing file to point to.

Remarks

Only available on Windows 2000 or newer.

Exceptions
Type Condition
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.

CreateSymlink(String, String)

Creates a symbolic link for a file or directory.

Declaration
public static void CreateSymlink(string sourcePath, string targetPath)
Parameters
Type Name Description
String sourcePath

The path of the link to create.

String targetPath

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

Exceptions
Type Condition
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.

Declaration
public static long GetFileID(string path)
Parameters
Type Name Description
String path

The path of the file.

Returns
Type Description
Int64
Exceptions
Type Condition
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.

GetNetFxDirectory(String)

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

Declaration
public static string GetNetFxDirectory(string version)
Parameters
Type Name Description
String version

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

Returns
Type Description
String

The path to the .NET Framework root directory.

Remarks

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

IsSymlink(String)

Checks whether a file is an NTFS symbolic link.

Declaration
public static bool IsSymlink(string path)
Parameters
Type Name Description
String path

The path of the file to check.

Returns
Type Description
Boolean

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

Remarks

Will return false for non-existing files.

Exceptions
Type Condition
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.

IsSymlink(String, out String)

Checks whether a file is an NTFS symbolic link.

Declaration
public static bool IsSymlink(string path, out string target)
Parameters
Type Name Description
String path

The path of the file to check.

String target

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

Returns
Type Description
Boolean

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

Exceptions
Type Condition
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.

Declaration
public static void MoveFileOnReboot(string sourcePath, string destinationPath)
Parameters
Type Name Description
String sourcePath

The source path to move the file from.

String destinationPath

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.

Declaration
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.

Declaration
public static void NotifyEnvironmentChanged()

ReadAllBytes(String)

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

Declaration
public static byte[] ReadAllBytes(string path)
Parameters
Type Name Description
String path

The path of the file to read.

Returns
Type Description
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
Type Condition
PlatformNotSupportedException

This method is called on a platform other than Windows.

RegisterApplicationRestart(String)

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

Declaration
public static void RegisterApplicationRestart(string arguments)
Parameters
Type Name Description
String arguments

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.

Declaration
public static int RegisterWindowMessage(string message)
Parameters
Type Name Description
String message

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

Returns
Type Description
Int32

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

SetCurrentProcessAppID(String)

Sets the current process' explicit application user model ID.

Declaration
public static void SetCurrentProcessAppID(string appID)
Parameters
Type Name Description
String appID

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.

Declaration
public static string[] SplitArgs(string commandLine)
Parameters
Type Name Description
String commandLine

The command-line to be split.

Returns
Type Description
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.

Declaration
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.

Declaration
public static void WriteAllBytes(string path, byte[] data)
Parameters
Type Name Description
String path

The path of the file to write to.

Byte[] data

The data to write to the file.

Remarks

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

Exceptions
Type Condition
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.

In This Article
Back to top Copyright Bastian Eicher