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
FSCTL_GET_REPARSE_POINT
Command to get the reparse point data block.
protected const int FSCTL_GET_REPARSE_POINT = 589992
Field Value
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
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
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
loggerILoggerLogger instance.
junctionPointstringThe junction point path.
targetDirstringThe target directory.
isOverwritingboolIf 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
isOverwritingis 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
lpFileNamestringFile name or path.
dwDesiredAccessJunctionPoint.EFileAccessDesired access flags.
dwShareModeJunctionPoint.EFileShareShare mode flags.
lpSecurityAttributesnintSecurity attributes pointer.
dwCreationDispositionJunctionPoint.ECreationDispositionCreation disposition.
dwFlagsAndAttributesJunctionPoint.EFileAttributesFlags and attributes.
hTemplateFilenintTemplate 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
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
hDevicenintDevice handle.
dwIoControlCodeuintI/O control code.
InBuffernintInput buffer pointer.
nInBufferSizeintInput buffer size.
OutBuffernintOutput buffer pointer.
nOutBufferSizeintOutput buffer size.
pBytesReturnedintNumber of bytes returned.
lpOverlappednintOverlapped 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
pathstringThe 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
loggerILoggerLogger instance.
handleSafeFileHandleSafe 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
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
pathstringPath 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
sourcestringSource path for the symbolic link.
targetstringTarget 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
sourcestringSource path for the hard link.
targetstringTarget 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
sourcestringSource path for the junction.
targetstringTarget 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
loggerILoggerLogger instance.
reparsePointPathstringPath to the reparse point.
accessModeJunctionPoint.EFileAccessFile 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
folderFromstringFolder 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
loggerILoggerLogger instance.
errorCodeintWin32 error code.
messagestringContext message for logging.
Returns
- bool
True if the error was access denied (silently handled).