Table of Contents

Class JunctionPoint

Namespace
SunamoWinStd
Assembly
SunamoWinStd.dll

Provides access to NTFS junction points in .Net.

public class JunctionPoint
Inheritance
JunctionPoint
Inherited Members
Extension Methods

Fields

ERROR_NOT_A_REPARSE_POINT

The file or directory is not a reparse point.

protected const int ERROR_NOT_A_REPARSE_POINT = 4390

Field Value

int

FSCTL_GET_REPARSE_POINT

Command to get the reparse point data block.

protected const int FSCTL_GET_REPARSE_POINT = 589992

Field Value

int

IO_REPARSE_TAG_MOUNT_POINT

Reparse point tag used to identify mount points and junction points.

protected const uint IO_REPARSE_TAG_MOUNT_POINT = 2684354563

Field Value

uint

NonInterpretedPathPrefix

??
This prefix indicates to NTFS that the path is to be treated as a non-interpreted path in the virtual file system.

protected const string NonInterpretedPathPrefix = "\\??\\"

Field Value

string

Methods

Create(ILogger, string, string, bool)

For files use mklink, this can be use only for directory. Creates a junction point from the specified directory to the specified target directory.

public static void Create(ILogger logger, string junctionPoint, string targetDir, bool isOverwriting)

Parameters

logger ILogger

Logger instance.

junctionPoint string

The junction point path.

targetDir string

The target directory.

isOverwriting bool

If true overwrites an existing reparse point or empty directory.

Remarks

Only works on NTFS.

Exceptions

IOException

Thrown when the junction point could not be created or when an existing directory was found and isOverwriting is false.

CreateFile(string, EFileAccess, EFileShare, nint, ECreationDisposition, EFileAttributes, nint)

Win32 CreateFile interop for opening reparse points.

protected static extern nint CreateFile(string lpFileName, JunctionPoint.EFileAccess dwDesiredAccess, JunctionPoint.EFileShare dwShareMode, nint lpSecurityAttributes, JunctionPoint.ECreationDisposition dwCreationDisposition, JunctionPoint.EFileAttributes dwFlagsAndAttributes, nint hTemplateFile)

Parameters

lpFileName string

File name or path.

dwDesiredAccess JunctionPoint.EFileAccess

Desired access flags.

dwShareMode JunctionPoint.EFileShare

Share mode flags.

lpSecurityAttributes nint

Security attributes pointer.

dwCreationDisposition JunctionPoint.ECreationDisposition

Creation disposition.

dwFlagsAndAttributes JunctionPoint.EFileAttributes

Flags and attributes.

hTemplateFile nint

Template file handle.

Returns

nint

Handle to the opened file.

Delete(ILogger, string)

Deletes a junction point at the specified source directory along with the directory itself. Does nothing if the junction point does not exist.

public static void Delete(ILogger logger, string junctionPoint)

Parameters

logger ILogger

Logger instance.

junctionPoint string

The junction point path.

Remarks

Only works on NTFS.

DeviceIoControl(nint, uint, nint, int, nint, int, out int, nint)

Win32 DeviceIoControl interop for reparse point operations.

protected static extern bool DeviceIoControl(nint hDevice, uint dwIoControlCode, nint InBuffer, int nInBufferSize, nint OutBuffer, int nOutBufferSize, out int pBytesReturned, nint lpOverlapped)

Parameters

hDevice nint

Device handle.

dwIoControlCode uint

I/O control code.

InBuffer nint

Input buffer pointer.

nInBufferSize int

Input buffer size.

OutBuffer nint

Output buffer pointer.

nOutBufferSize int

Output buffer size.

pBytesReturned int

Number of bytes returned.

lpOverlapped nint

Overlapped structure pointer.

Returns

bool

True if the operation succeeded.

GetTarget(string)

Gets the target of the specified junction point. Works for /j and /d (folders). Does not work for /h (file) - see GetTargetTest. If the path is not /j, /d, or /h, returns null.

public static string? GetTarget(string path)

Parameters

path string

The junction point or reparse point path.

Returns

string

The target of the junction point, or null.

Remarks

Only works on NTFS.

Exceptions

IOException

Thrown when the specified path does not exist, is invalid, is not a junction point, or some other error occurs.

InternalGetTarget(ILogger, SafeFileHandle?)

Gets the target path from a reparse point handle.

protected static string? InternalGetTarget(ILogger logger, SafeFileHandle? handle)

Parameters

logger ILogger

Logger instance.

handle SafeFileHandle

Safe file handle to the reparse point.

Returns

string

The target path, or null if not a reparse point.

IsJunctionPoint(ILogger, string)

For normal folder and /H returns false. For junction returns true. Determines whether the specified path exists and refers to a junction point.

public static bool IsJunctionPoint(ILogger logger, string path)

Parameters

logger ILogger

Logger instance.

path string

The junction point path.

Returns

bool

True if the specified path represents a junction point.

Exceptions

IOException

Thrown if the specified path is invalid or some other error occurs.

IsReparsePoint(string)

Checks if the specified path is a reparse point (junction, symlink, etc.).

public static bool IsReparsePoint(string path)

Parameters

path string

Path to check.

Returns

bool

True if the path is a reparse point.

MklinkD(string, string)

Creates a directory symbolic link (/D) using mklink. Only works for directories.

public static string MklinkD(string source, string target)

Parameters

source string

Source path for the symbolic link.

target string

Target directory path that the link points to.

Returns

string

The mklink command string.

MklinkH(string, string)

Creates a hard link (/H) using mklink. Only works for files. /H always creates a file link (recognizable in file system). If the target exists, it will be overwritten.

public static string MklinkH(string source, string target)

Parameters

source string

Source path for the hard link.

target string

Target file path that the link points to.

Returns

string

The mklink command string.

MklinkJ(string, string)

Creates a junction (/J) using mklink. Only works for directories.

public static string MklinkJ(string source, string target)

Parameters

source string

Source path for the junction.

target string

Target directory path that the junction points to.

Returns

string

The mklink command string.

OpenReparsePoint(ILogger, string, EFileAccess)

Opens a reparse point for reading or writing. Cannot be used for /H (hard links).

protected static SafeFileHandle? OpenReparsePoint(ILogger logger, string reparsePointPath, JunctionPoint.EFileAccess accessMode)

Parameters

logger ILogger

Logger instance.

reparsePointPath string

Path to the reparse point.

accessMode JunctionPoint.EFileAccess

File access mode.

Returns

SafeFileHandle

Safe file handle to the reparse point, or null on error.

PathsAndTargetsOfAll(string)

Gets paths and targets of all junction points in the specified folder.

public static Dictionary<string, string> PathsAndTargetsOfAll(string folderFrom)

Parameters

folderFrom string

Folder to scan for junction points.

Returns

Dictionary<string, string>

Dictionary mapping junction point paths to their targets.

ThrowLastWin32Error(ILogger, int, string)

Handles a Win32 error. Returns true for access denied (error 5), otherwise logs the error.

protected static bool ThrowLastWin32Error(ILogger logger, int errorCode, string message)

Parameters

logger ILogger

Logger instance.

errorCode int

Win32 error code.

message string

Context message for logging.

Returns

bool

True if the error was access denied (silently handled).