I'm trying to create an NTFS Junction. From the cmd line I can do this using the junction.exe tool from sysinternals. The output of a DIR cmd for a junction looks like this:
Volume in drive C has no label.
Volume Serial Number is C8BC-2EBD
Directory of c:\users\cheeso\Documents
03/22/2009 09:45 PM <JUNCTION> My Music [\??\c:\users\cheeso\Music]
05/11/2007 05:42 PM <DIR> My Received Files
03/22/2009 09:46 PM <JUNCTION> my videos [\??\c:\users\cheeso\Videos]
I read somewhere that Junctions are a subset of Symbolic Links.
So I tried using CreateSymbolicLink to create a Junction. When I do this, I actually get a Symlink, not a junction.
09/09/2009 11:50 AM <SYMLINKD> newLink [.\]
There is also CreateHardLink. The doc there says junctions (aka "Reparse Points") are a subset of hardlinks. but I can't seem to get this call to work. It completes but there is no hardlink or junction created.
I'm using .NET/C# and the imports look like this:
[Interop.DllImport("kernel32.dll", EntryPoint="CreateSymbolicLinkW", CharSet=Interop.CharSet.Unicode)]
public static extern int CreateSymbolicLink(string lpSymlinkFileName, string lpTargetFileName, int dwFlags);
[Interop.DllImport("kernel32.dll", EntryPoint="CreateHardLinkW", CharSet=Interop.CharSet.Unicode)]
public static extern bool CreateHardLink(string lpFileName,
string lpExistingFileName,
IntPtr mustBeNull);
What am I doing wrong?
How can I create a Junction from within C#?
Junctions links work only for the absolute path. Symbolic Links works for both absolute and relative path. Symbolic Links works for local as well as remote paths. Creating junction links does not require any special permissions, and Windows Standard User is sufficient.
A junction, also called an NTFS junction point, is a feature of the NTFS file system. It is pointer to a directory on the local volume, similar to a symlink. It can be accessed through the Windows GUI in addition to the Windows command line.
A symbolic link, as created by Windows, is much similar to a directory junction, but unlike a directory junction it can point to a file or a remote network file or directory. The target may be defined as a path relative to the symbolic link position, or an absolute path in the current volume or another one.
It looks like you can, and somebody's created a library on CodeProject that has a number of functions they've built in C# to work with Junction points.
http://www.codeproject.com/KB/files/JunctionPointsNet.aspx
It looks like he's actually using the following DllImport to accomplish it:
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern bool DeviceIoControl(IntPtr hDevice, uint dwIoControlCode,
IntPtr InBuffer, int nInBufferSize,
IntPtr OutBuffer, int nOutBufferSize,
out int pBytesReturned, IntPtr lpOverlapped);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With