Search code examples
c#.netdirectorynetwork-drivemklink

How can I identify if a directory is a symbolic link (in contrast to a "real" local directory)?


I need to block access to network based directories. However by using tools like "mklink" users can create mapped directories which appear like local directories. I use a .net based code to get the UNC paths of the directories to identify network folders. But this does not work for mapped folders. Is there any information in DirectoryInfo class which I can use?


Solution

  • Disclaimer: I haven't tested symlinks on a network drive - don't have permission to create one. I'll try at home later and update my answer. But I can tell you that on a local disk:

    • For a "normal" directory, the Attributes property will have the Directory bit set.
    • For a symlink -- I'm guessing for a junction as well -- the Directory and ReparsePoint bits are set.

    So try checking the ReparsePoint bit.

    UPDATE:
    A network folder has only the Directory bit set.
    There seems to be some oddness about the root directory on a volume -- whether it's a local or network drive -- it also has the Hidden and System bits set. Not sure why.
    But it does seem that network folders behave just like local folders (which makes sense).