NanoByte.Common  2.6.1
NanoByte.Common.Native.WindowsUtils Class Reference

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

Static Public Member Functions

static string GetNetFxDirectory (string version)
 Returns the .NET Framework root directory for a specific version of the .NET Framework. Does not verify the directory actually exists! More...
 
static string[] SplitArgs (string? commandLine)
 Tries to split a command-line into individual arguments. More...
 
static bool AttachConsole ()
 Tries to attach to a command-line console owned by the parent process. More...
 
static ? byte[] ReadAllBytes ([Localizable(false)] string path)
 Reads the entire contents of a file using the Win32 API. More...
 
static void WriteAllBytes ([Localizable(false)] string path, byte[] data)
 Writes the entire contents of a byte array to a file using the Win32 API. Existing files with the same name are overwritten. More...
 
static void CreateSymlink ([Localizable(false)] string sourcePath, [Localizable(false)] string targetPath)
 Creates a symbolic link for a file or directory. More...
 
static bool IsSymlink ([Localizable(false)] string path)
 Checks whether a file is an NTFS symbolic link. More...
 
static bool IsSymlink ([Localizable(false)] string path, [NotNullWhen(true)] out string? target)
 Checks whether a file is an NTFS symbolic link. More...
 
static void CreateHardlink ([Localizable(false)] string sourcePath, [Localizable(false)] string targetPath)
 Creates a hard link between two files. More...
 
static bool AreHardlinked ([Localizable(false)] string path1, [Localizable(false)] string path2)
 Determines whether to files are hardlinked. More...
 
static void MoveFileOnReboot (string sourcePath, string? destinationPath)
 Moves a file on the next reboot of the OS. Replaces existing files. More...
 
static void SetCurrentProcessAppID (string appID)
 Sets the current process' explicit application user model ID. More...
 
static void NotifyAssocChanged ()
 Informs the Windows shell that changes were made to the file association data in the registry. More...
 
static void NotifyEnvironmentChanged ()
 Informs all GUI applications that changes where made to the environment variables (e.g. PATH) and that they should re-pull them. More...
 
static int RegisterWindowMessage ([Localizable(false)] string message)
 Registers a new message type that can be sent to windows. More...
 
static void BroadcastMessage (int messageID)
 Sends a message of a specific type to all windows in the current session. More...
 
static void RegisterApplicationRestart (string arguments)
 Registers the current application for automatic restart after updates or crashes. More...
 
static void UnregisterApplicationRestart ()
 Unregisters the current application for automatic restart after updates or crashes. More...
 

Static Public Attributes

const string NetFx20 = "v2.0.50727"
 The directory version number for .NET Framework 2.0. This release includes the C# 2.0 compiler and the CLR 2.0 runtime. More...
 
const string NetFx30 = "v3.0"
 The directory version number for .NET Framework 3.0. More...
 
const string NetFx35 = "v3.5"
 The directory version number for .NET Framework 3.5. This release includes the C# 3.0 compiler. More...
 
const string NetFx40 = "v4.0.30319"
 The directory version number for .NET Framework 4.x. This release includes a C# 4.0+ compiler and the CLR 4.0 runtime. More...
 

Static Package Functions

static Exception BuildException (int error)
 Builds a suitable Exception for a given Win32Exception.NativeErrorCode. More...
 

Static Package Attributes

const int Win32ErrorFileNotFound = 2
 The Win32Exception.NativeErrorCode value indicating that a file was not found. More...
 
const int Win32ErrorAccessDenied = 5
 The Win32Exception.NativeErrorCode value indicating that access to a resource was denied. More...
 
const int Win32ErrorWriteFault = 29
 The Win32Exception.NativeErrorCode value indicating that write access to a resource failed. More...
 
const int Win32ErrorSemTimeout = 121
 The Win32Exception.NativeErrorCode value indicating that an operation timed out. More...
 
const int Win32ErrorAlreadyExists = 183
 The Win32Exception.NativeErrorCode value indicating that an element (e.g. a file) already exists. More...
 
const int Win32ErrorMoreData = 234
 The Win32Exception.NativeErrorCode value indicating that more data is available and the query should be repeated with a larger output buffer/array. More...
 
const int Win32ErrorRequestedOperationRequiresElevation = 740
 The Win32Exception.NativeErrorCode value indicating that the requested application needs UAC elevation. More...
 
const int Win32ErrorCancelled = 1223
 The Win32Exception.NativeErrorCode value indicating that an operation was cancelled by the user. More...
 

Properties

static bool IsWindows [get]
 true if the current operating system is Windows (9x- or NT-based); false otherwise. More...
 
static bool IsWindowsNT [get]
 true if the current operating system is a modern Windows version (NT-based); false otherwise. More...
 
static bool IsWindowsXP [get]
 true if the current operating system is Windows XP or newer; false otherwise. More...
 
static bool IsWindowsVista [get]
 true if the current operating system is Windows Vista or newer; false otherwise. More...
 
static bool IsWindows7 [get]
 true if the current operating system is Windows 7 or newer; false otherwise. More...
 
static bool IsWindows8 [get]
 true if the current operating system is Windows 8 or newer; false otherwise. More...
 
static bool IsWindows10 [get]
 true if the current operating system is Windows 10 or newer; false otherwise. More...
 
static bool IsWindows10Redstone [get]
 true if the current operating system is Windows 10 Anniversary Update (Redstone 1) or newer; false otherwise. More...
 
static bool HasUac [get]
 true if the current operating system supports UAC and it is enabled; false otherwise. More...
 
static bool IsAdministrator [get]
 Indicates whether the current user is an administrator. Always returns true on non-Windows NT systems. More...
 
static bool IsInteractive [get]
 Indicates whether the current process is running in an interactive session (rather than, e.g. as a service). Always returns true on non-Windows NT systems. More...
 
static string CurrentProcessPath [get]
 Determines the path of the executable the current process was launched from. More...
 
static double AbsoluteTime [get]
 A time index in seconds that continuously increases. More...
 

Detailed Description

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

Member Function Documentation

◆ AreHardlinked()

static bool NanoByte.Common.Native.WindowsUtils.AreHardlinked ( [Localizable(false)] string  path1,
[Localizable(false)] string  path2 
)
inlinestatic

Determines whether to files are hardlinked.

Parameters
path1The path of the first file.
path2The path of the second file.
Exceptions
IOExceptionThere was an IO problem checking the files.
UnauthorizedAccessExceptionYou have insufficient rights to check the files.
Win32ExceptionChecking the files failed.
PlatformNotSupportedExceptionThis method is called on a platform other than Windows NT.

◆ AttachConsole()

static bool NanoByte.Common.Native.WindowsUtils.AttachConsole ( )
static

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

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

◆ BroadcastMessage()

static void NanoByte.Common.Native.WindowsUtils.BroadcastMessage ( int  messageID)
inlinestatic

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

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

◆ BuildException()

static Exception NanoByte.Common.Native.WindowsUtils.BuildException ( int  error)
inlinestaticpackage

Builds a suitable Exception for a given Win32Exception.NativeErrorCode.

◆ CreateHardlink()

static void NanoByte.Common.Native.WindowsUtils.CreateHardlink ( [Localizable(false)] string  sourcePath,
[Localizable(false)] string  targetPath 
)
inlinestatic

Creates a hard link between two files.

Parameters
sourcePathThe path of the link to create.
targetPathThe absolute path of the existing file to point to.

Only available on Windows 2000 or newer.

Exceptions
IOExceptionThere was an IO problem creating the hard link.
UnauthorizedAccessExceptionYou have insufficient rights to create the hard link.
Win32ExceptionThe hard link creation failed.
PlatformNotSupportedExceptionThis method is called on a platform other than Windows NT.

◆ CreateSymlink()

static void NanoByte.Common.Native.WindowsUtils.CreateSymlink ( [Localizable(false)] string  sourcePath,
[Localizable(false)] string  targetPath 
)
inlinestatic

Creates a symbolic link for a file or directory.

Parameters
sourcePathThe path of the link to create.
targetPathThe path of the existing file or directory to point to (relative to sourcePath ).
Exceptions
IOExceptionThere was an IO problem creating the symlink.
UnauthorizedAccessExceptionYou have insufficient rights to create the symbolic link.
Win32ExceptionThe symbolic link creation failed.
PlatformNotSupportedExceptionThis method is called on a platform other than Windows NT 6.0 (Vista) or newer.

◆ GetNetFxDirectory()

static string NanoByte.Common.Native.WindowsUtils.GetNetFxDirectory ( string  version)
inlinestatic

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

Parameters
versionThe full .NET version number including the leading "v". Use predefined constants when possible.
Returns
The path to the .NET Framework root directory.

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

◆ IsSymlink() [1/2]

static bool NanoByte.Common.Native.WindowsUtils.IsSymlink ( [Localizable(false)] string  path)
inlinestatic

Checks whether a file is an NTFS symbolic link.

Parameters
pathThe path of the file to check.
Returns
true if path points to a symbolic link; false otherwise.

Will return false for non-existing files.

Exceptions
IOExceptionThere was an IO problem getting link information.
UnauthorizedAccessExceptionYou have insufficient rights to get link information.
Win32ExceptionGetting link information failed.
PlatformNotSupportedExceptionThis method is called on a platform other than Windows NT 6.0 (Vista) or newer.

◆ IsSymlink() [2/2]

static bool NanoByte.Common.Native.WindowsUtils.IsSymlink ( [Localizable(false)] string  path,
[NotNullWhen(true)] out string?  target 
)
inlinestatic

Checks whether a file is an NTFS symbolic link.

Parameters
pathThe path of the file to check.
targetReturns the target the symbolic link points to if it exists.
Returns
true if path points to a symbolic link; false otherwise.
Exceptions
IOExceptionThere was an IO problem getting link information.
UnauthorizedAccessExceptionYou have insufficient rights to get link information.
Win32ExceptionGetting link information failed.
PlatformNotSupportedExceptionThis method is called on a platform other than Windows NT 6.0 (Vista) or newer.

◆ MoveFileOnReboot()

static void NanoByte.Common.Native.WindowsUtils.MoveFileOnReboot ( string  sourcePath,
string?  destinationPath 
)
inlinestatic

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

Parameters
sourcePathThe source path to move the file from.
destinationPathThe destination path to move the file to. null to delete the file instead of moving it.

Useful for replacing in-use files.

◆ NotifyAssocChanged()

static void NanoByte.Common.Native.WindowsUtils.NotifyAssocChanged ( )
inlinestatic

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

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

◆ NotifyEnvironmentChanged()

static void NanoByte.Common.Native.WindowsUtils.NotifyEnvironmentChanged ( )
inlinestatic

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

◆ ReadAllBytes()

static ? byte [] NanoByte.Common.Native.WindowsUtils.ReadAllBytes ( [Localizable(false)] string  path)
inlinestatic

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

Parameters
pathThe path of the file to read.
Returns
The contents of the file as a byte array; null if there was a problem reading the file.
Exceptions
PlatformNotSupportedExceptionThis method is called on a platform other than Windows.

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

◆ RegisterApplicationRestart()

static void NanoByte.Common.Native.WindowsUtils.RegisterApplicationRestart ( string  arguments)
inlinestatic

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

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

◆ RegisterWindowMessage()

static int NanoByte.Common.Native.WindowsUtils.RegisterWindowMessage ( [Localizable(false)] string  message)
static

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

Parameters
messageA unique string used to identify the message type session-wide.
Returns
A unique ID number used to identify the message type session-wide.

◆ SetCurrentProcessAppID()

static void NanoByte.Common.Native.WindowsUtils.SetCurrentProcessAppID ( string  appID)
inlinestatic

Sets the current process' explicit application user model ID.

Parameters
appIDThe application ID to set.

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

◆ SplitArgs()

static string [] NanoByte.Common.Native.WindowsUtils.SplitArgs ( string?  commandLine)
inlinestatic

Tries to split a command-line into individual arguments.

Parameters
commandLineThe command-line to be split.
Returns
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()

static void NanoByte.Common.Native.WindowsUtils.UnregisterApplicationRestart ( )
inlinestatic

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

◆ WriteAllBytes()

static void NanoByte.Common.Native.WindowsUtils.WriteAllBytes ( [Localizable(false)] string  path,
byte[]  data 
)
inlinestatic

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

Parameters
pathThe path of the file to write to.
dataThe data to write to the file.
Exceptions
IOExceptionThere was an IO problem writing the file.
UnauthorizedAccessExceptionWrite access to the file was denied.
Win32ExceptionThere was a problem writing the file.
PlatformNotSupportedExceptionThis method is called on a platform other than Windows.

This method works like File.WriteAllBytes, but bypasses .NET's file path validation logic.

Member Data Documentation

◆ NetFx20

const string NanoByte.Common.Native.WindowsUtils.NetFx20 = "v2.0.50727"
static

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

◆ NetFx30

const string NanoByte.Common.Native.WindowsUtils.NetFx30 = "v3.0"
static

The directory version number for .NET Framework 3.0.

◆ NetFx35

const string NanoByte.Common.Native.WindowsUtils.NetFx35 = "v3.5"
static

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

◆ NetFx40

const string NanoByte.Common.Native.WindowsUtils.NetFx40 = "v4.0.30319"
static

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

◆ Win32ErrorAccessDenied

const int NanoByte.Common.Native.WindowsUtils.Win32ErrorAccessDenied = 5
staticpackage

The Win32Exception.NativeErrorCode value indicating that access to a resource was denied.

◆ Win32ErrorAlreadyExists

const int NanoByte.Common.Native.WindowsUtils.Win32ErrorAlreadyExists = 183
staticpackage

The Win32Exception.NativeErrorCode value indicating that an element (e.g. a file) already exists.

◆ Win32ErrorCancelled

const int NanoByte.Common.Native.WindowsUtils.Win32ErrorCancelled = 1223
staticpackage

The Win32Exception.NativeErrorCode value indicating that an operation was cancelled by the user.

◆ Win32ErrorFileNotFound

const int NanoByte.Common.Native.WindowsUtils.Win32ErrorFileNotFound = 2
staticpackage

The Win32Exception.NativeErrorCode value indicating that a file was not found.

◆ Win32ErrorMoreData

const int NanoByte.Common.Native.WindowsUtils.Win32ErrorMoreData = 234
staticpackage

The Win32Exception.NativeErrorCode value indicating that more data is available and the query should be repeated with a larger output buffer/array.

◆ Win32ErrorRequestedOperationRequiresElevation

const int NanoByte.Common.Native.WindowsUtils.Win32ErrorRequestedOperationRequiresElevation = 740
staticpackage

The Win32Exception.NativeErrorCode value indicating that the requested application needs UAC elevation.

◆ Win32ErrorSemTimeout

const int NanoByte.Common.Native.WindowsUtils.Win32ErrorSemTimeout = 121
staticpackage

The Win32Exception.NativeErrorCode value indicating that an operation timed out.

◆ Win32ErrorWriteFault

const int NanoByte.Common.Native.WindowsUtils.Win32ErrorWriteFault = 29
staticpackage

The Win32Exception.NativeErrorCode value indicating that write access to a resource failed.

Property Documentation

◆ AbsoluteTime

double NanoByte.Common.Native.WindowsUtils.AbsoluteTime
staticget

A time index in seconds that continuously increases.

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

◆ CurrentProcessPath

string NanoByte.Common.Native.WindowsUtils.CurrentProcessPath
staticget

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

◆ HasUac

bool NanoByte.Common.Native.WindowsUtils.HasUac
staticget

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

◆ IsAdministrator

bool NanoByte.Common.Native.WindowsUtils.IsAdministrator
staticget

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

◆ IsInteractive

bool NanoByte.Common.Native.WindowsUtils.IsInteractive
staticget

Indicates whether the current process is running in an interactive session (rather than, e.g. as a service). Always returns true on non-Windows NT systems.

◆ IsWindows

bool NanoByte.Common.Native.WindowsUtils.IsWindows
staticget

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

◆ IsWindows10

bool NanoByte.Common.Native.WindowsUtils.IsWindows10
staticget

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

◆ IsWindows10Redstone

bool NanoByte.Common.Native.WindowsUtils.IsWindows10Redstone
staticget

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

◆ IsWindows7

bool NanoByte.Common.Native.WindowsUtils.IsWindows7
staticget

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

◆ IsWindows8

bool NanoByte.Common.Native.WindowsUtils.IsWindows8
staticget

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

◆ IsWindowsNT

bool NanoByte.Common.Native.WindowsUtils.IsWindowsNT
staticget

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

◆ IsWindowsVista

bool NanoByte.Common.Native.WindowsUtils.IsWindowsVista
staticget

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

◆ IsWindowsXP

bool NanoByte.Common.Native.WindowsUtils.IsWindowsXP
staticget

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


The documentation for this class was generated from the following file: