48

Is there a way to tell using C# if a file is real or a symbolic link?

I've dug through the MSDN W32 docs, and can't find anything for checking this. I'm using CreateSymbolicLink from here, and it's working fine.

9 Answers 9

63
private bool IsSymbolic(string path)
{
    FileInfo pathInfo = new FileInfo(path);
    return pathInfo.Attributes.HasFlag(FileAttributes.ReparsePoint);
}
4
  • 5
    This should be the accepted answer. It's simple, concise, and directly answers the question.
    – Jim Gomes
    Commented Mar 30, 2016 at 22:23
  • Is there something I am missing about this solution vs the accepted solution because this seems a lot nicer.
    – Max Young
    Commented Oct 11, 2016 at 17:29
  • 19
    Just because a file has a reparse point associated with it does NOT mean it's a symbolic link. A reparse point is just an arbitrary set of custom data associated with a file. You need to inspect the ID of the reparse point data to determine if it actually defines a symbolic link. This answer will give false positives anytime it encounters a real file with reparse points. See here: msdn.microsoft.com/en-us/library/windows/desktop/… Commented Nov 30, 2016 at 22:13
  • 1
    Indeed. Read the last line: msdn.microsoft.com/en-us/library/aa363940.aspx
    – konsolebox
    Commented May 10, 2017 at 5:36
26

I have some source code for symlinks posted on my blog that will allow you to:

  • create symlinks
  • check whether a path is a symlink
  • retrieve the target of a symlink

It also contains NUnit test cases, that you may wish to extend.

The meaty bit is:

private static SafeFileHandle getFileHandle(string path)
{
    return CreateFile(path, genericReadAccess, shareModeAll, IntPtr.Zero, openExisting,
        fileFlagsForOpenReparsePointAndBackupSemantics, IntPtr.Zero);
}

public static string GetTarget(string path)
{
    SymbolicLinkReparseData reparseDataBuffer;

    using (SafeFileHandle fileHandle = getFileHandle(path))
    {
        if (fileHandle.IsInvalid)
        {
            Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error());
        }

        int outBufferSize = Marshal.SizeOf(typeof(SymbolicLinkReparseData));
        IntPtr outBuffer = IntPtr.Zero;
        try
        {
            outBuffer = Marshal.AllocHGlobal(outBufferSize);
            int bytesReturned;
            bool success = DeviceIoControl(
                fileHandle.DangerousGetHandle(), ioctlCommandGetReparsePoint, IntPtr.Zero, 0,
                outBuffer, outBufferSize, out bytesReturned, IntPtr.Zero);

            fileHandle.Close();

            if (!success)
            {
                if (((uint)Marshal.GetHRForLastWin32Error()) == pathNotAReparsePointError)
                {
                    return null;
                }
                Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error());
            }

            reparseDataBuffer = (SymbolicLinkReparseData)Marshal.PtrToStructure(
                outBuffer, typeof(SymbolicLinkReparseData));
        }
        finally
        {
            Marshal.FreeHGlobal(outBuffer);
        }
    }
    if (reparseDataBuffer.ReparseTag != symLinkTag)
    {
        return null;
    }

    string target = Encoding.Unicode.GetString(reparseDataBuffer.PathBuffer,
        reparseDataBuffer.PrintNameOffset, reparseDataBuffer.PrintNameLength);

    return target;
}

That is:

5
12

Starting with .NET 6 you can use: FileSystemInfo.LinkTarget Property

Property description:

Gets the target path of the link located in FullName, or null if this FileSystemInfo instance doesn't represent a link.

For example:

static bool IsSymbolicLink(string path)
{
    FileInfo file = new FileInfo(path);
    return file.LinkTarget != null;
}
1
  • 3
    This is new as of .NET 6, and should be the preferred way now. Thanks! Commented Nov 7, 2022 at 17:55
6

Here is an example of differentiating files and directories from links to files and links to directories.

Links to either files or directories maintain their own attributes (creation date, permissions) separate from their targets.

File links can be deleted (e.g. using "del") without affecting the target file.

Directory links can be removed (e.g. "rmdir") without affecting the target directory. Take care when using "rd /s". This WILL remove the directory link target.

The key FileAttributes flag to check in both FileInfo and DirectoryInfo is FileAttributes.ReparsePoint.

static void Main( string[] args ) {
FileInfo file_info = new FileInfo(args[0]);
DirectoryInfo directory_info = new DirectoryInfo(args[0]);

bool is_file = file_info.Exists;
bool is_directory = directory_info.Exists;

if (is_file) {
    Console.WriteLine(file_info.ToString() + " is a file");

    if ( file_info.Attributes.HasFlag(FileAttributes.ReparsePoint) )
        Console.WriteLine(args[0] + " is a Windows file link");
}
else if (is_directory) {
    Console.WriteLine(directory_info.ToString() + " is a directory");

    if ( directory_info.Attributes.HasFlag(FileAttributes.ReparsePoint) )
        Console.WriteLine(args[0] + " is a Windows directory link");
}
2
  • 1
    Care to add a description? Code-only is sometimes okay but a few words to describe your answer can go a long way. Commented Feb 4, 2014 at 16:57
  • 7
    Again, relying on FileAttributes.ReparsePoint is not enough.
    – konsolebox
    Commented May 10, 2017 at 5:38
4

It proves the above answers are not reliable. Finally I got the right solution from MSDN:

To determine if a specified directory is a mounted folder, first call the GetFileAttributes function and inspect the FILE_ATTRIBUTE_REPARSE_POINT flag in the return value to see if the directory has an associated reparse point. If it does, use the FindFirstFile and FindNextFile functions to obtain the reparse tag in the dwReserved0 member of the WIN32_FIND_DATA structure. To determine if the reparse point is a mounted folder (and not some other form of reparse point), test whether the tag value equals the value IO_REPARSE_TAG_MOUNT_POINT. For more information, see Reparse Points.

1
  • 1
    Wow this is the first time I've ever come across a "reserved" field having a documented use without being renamed. Commented Apr 9, 2021 at 2:59
0

GetFileInformationByHandle fills a BY_HANDLE_FILE_INFORMATION structure which has a field dwFileAttributes where bits are set with info about the file's attributes (details here). In particular, look at the bit at mask...:

FILE_ATTRIBUTE_REPARSE_POINT 1024 0x0400

A file or directory that has an associated reparse point, or a file that is a symbolic link.

6
  • I've tried using the System.IO.File.GetAttributes() method, which I believe implements this, but it only seems to work on Junction Points, and not Symbolic Links.
    – mattdwen
    Commented Sep 28, 2009 at 4:50
  • Can you try the syscall itself? I have no Vista install at hand to try this myself. Commented Sep 28, 2009 at 4:56
  • Same thing - getting 32, which is Archive only. I've just found out I may have to abort this method and use Hard Links anyway, but it would be good to figure it.
    – mattdwen
    Commented Sep 28, 2009 at 8:07
  • Ah well - looks like MSDN is incorrect on this point, then:-(. Commented Sep 28, 2009 at 14:19
  • And I'm getting the same thing for Hard Links as well. Never shows it as a reparse point.
    – mattdwen
    Commented Sep 28, 2009 at 20:54
0

According to this answer to Stack Overflow question Find out whether a file is a symbolic link in PowerShell, getting the System.IO.FileAttributes for the file (via File.GetAttributes), and testing for the ReparsePoint bit, works. If the bit is set, it is a symlink or a junction point. If not, it is a regular file (or hardlink).

1
  • 1
    Again, relying on ReparsePoint is not enough.
    – Joshua
    Commented Jan 12, 2018 at 22:32
0

The library MonoPosix provides API to check if a file is a symbolic link:

public bool IsSymlink(string filePath)
   => UnixFileSystemInfo.GetFileSystemEntry(filePath).IsSymbolicLink;
0

I know I am late to the party but found this discussion when researching same question

I found the below worked for me so thought I would post in case of use to anyone else

It works like this:-

var provider = ReparsePointFactory.Provider;

var link = provider.GetLink(@"c:\program files (x86)\common files\microsoft shared\vgx\vgx.dll");

MsgBox("Link Type: " + link.Type.ToString + " Link Target: " + link.Target + " Link Attributes: " + link.Attributes.ToString);

https://github.com/NCodeGroup/NCode.ReparsePoints https://www.nuget.org/packages/NCode.ReparsePoints/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.