2 Replies - 1184 Views - Last Post: 25 July 2012 - 12:26 PM Rate Topic: -----

#1 blank_program  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 11
  • View blog
  • Posts: 277
  • Joined: 22-July 09

Creating/Checking Symlinks in Windows 7

Posted 24 July 2012 - 12:37 PM

I am getting back into writing some small applications while I have fresh ideas and the first one I want to do involves symbolic links or NTFS junction points. Since Vista/7/Server 2008 have this functionality built in I thought about just passing the command through the command prompt to reach my goal but I don't feel this is the right way to do it.

I found some code on Google I was trying to reference, not copy/paste it, to figure it out but am having a hard time. So I am wondering if anyone has any links to tutorials for creating symbolic link/junction points and possibly also checking if a target is one?

For instance it would not be nessesary to symlink to a symlink, at least in my mind, so I would like to display a dialog if this happens. I know I need P/Invoke which I don't have experience with but am looking to learn.

Is This A Good Question/Topic? 0
  • +

Replies To: Creating/Checking Symlinks in Windows 7

#2 Curtis Rutland  Icon User is online

  • (╯°□°)╯︵ (~ .o.)~
  • member icon


Reputation: 3803
  • View blog
  • Posts: 6,410
  • Joined: 08-June 10

Re: Creating/Checking Symlinks in Windows 7

Posted 25 July 2012 - 10:48 AM

Well, here's a start for you:

Creating Symbolic Links
CreateSymbolicLink Win32 function
pinvoke.net's CreateSymbolicLink page

If you've never done any p/invoking, it's not really that big of a deal. You have to know a bit about unmanaged code, but pinvoke.net usually gives you what you need.

Here's an example for this case:

internal static class Win32
{
    [DllImport("kernel32.dll", SetLastError = true)]
    [return: MarshalAs(UnmanagedType.I1)]
    public static extern bool CreateSymbolicLink(string lpSymlinkFileName, string lpTargetFileName, SymLinkFlag dwFlags);

    internal enum SymLinkFlag
    {
        File = 0,
        Directory = 1
    }
}



In this case, we're telling the compiler that the method CreateSymbolicLink is elsewhere, defined in kernel32.dll. We're also letting the platform know to store the last Win32 error if it happens (we'll look for it later), and we're telling the platform how to marshal the return value. Since the return value is a 1 byte value (and the default marshalling for bools is 4 bytes) we're having to explain to the platform how we want to handle the result.

And just like that, you can call into the Win32 API.

Here's an example of how I used this:

class Program
{
    static void Main(string[] args)
    {
        CreateSymLink(@"c:\somefile.txt", @"c:\dev\somefile.txt");
    }

    public static void CreateSymLink(string name, string target, bool isDirectory = false)
    {
        if (!Win32.CreateSymbolicLink(name, target, isDirectory ? Win32.SymLinkFlag.Directory : Win32.SymLinkFlag.File))
        {
            throw new Win32Exception();
        }
    }
}



Mostly self explanatory. I wrapped it with another method that has a default value. It makes the call, and if the return value is false, it throws a Win32Exception (which uses the last Win32 error by default. That's why we used SetLastError=true).

As to determining whether a file is or isn't a symlink or junction point, read up here:

http://stackoverflow...a-symbolic-link
Was This Post Helpful? 0
  • +
  • -

#3 blank_program  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 11
  • View blog
  • Posts: 277
  • Joined: 22-July 09

Re: Creating/Checking Symlinks in Windows 7

Posted 25 July 2012 - 12:26 PM

Thanks, that post was very helpful explaining the code. I will give it a shot and see what happens. I think for the rest of my program I should be fine except for multithreading in WPF to fill a progress bar in. However that is for another topic and time.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1