views:

406

answers:

1

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#?

+4  A: 

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);
rwmnau
perfect, thanks! I looked and found a few links but not that one, and nothing that was really usable.
Cheeso