Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get real path from symlink C#

Tags:

c#

Does anybody know how can get real path from symlink file or folder? Thank you!

like image 545
Emil Eremiev Avatar asked Jul 11 '16 05:07

Emil Eremiev


People also ask

How do I find the path of a symbolic link?

ls command to find a symbolic link in UNIX systems If you combine the output of the ls command with grep and use a regular expression to find all entries which start with a small L then you can easily find all soft links on any directories.

How do I bypass Symlinks?

Overwriting Symlinks If you try to create a symbolic link that already exists , the ln command will print an error message. To overwrite the destination path of the symlink, use the -f ( --force ) option.

How do you find the source of a symbolic linked file in Linux?

Long answer: to find the name of the actual file/folder that a symbolic link points to, check the info after the -> in the ls -l command. The link passwd in your example is a relative link.

Can you chain Symlinks?

Yes. You can only stack so much symbolic links together before the kernel and/or application refuse to follow the chain.


1 Answers

Hello guys after my research I found this solution for how to get real path of a Symlink. If you have a created symlink and want to check where is the real pointer of this file or folder. If someone have better way to write it please share.

    [DllImport("kernel32.dll", EntryPoint = "CreateFileW", CharSet = CharSet.Unicode, SetLastError = true)]
    private static extern SafeFileHandle CreateFile(string lpFileName, int dwDesiredAccess, int dwShareMode, IntPtr securityAttributes, int dwCreationDisposition, int dwFlagsAndAttributes, IntPtr hTemplateFile);

    [DllImport("kernel32.dll", EntryPoint = "GetFinalPathNameByHandleW", CharSet = CharSet.Unicode, SetLastError = true)]
    private static extern int GetFinalPathNameByHandle([In] SafeFileHandle hFile, [Out] StringBuilder lpszFilePath, [In] int cchFilePath, [In] int dwFlags);

    private const int CREATION_DISPOSITION_OPEN_EXISTING = 3;
    private const int FILE_FLAG_BACKUP_SEMANTICS = 0x02000000;


    public static string GetRealPath(string path)
    {
        if (!Directory.Exists(path) && !File.Exists(path))
        {
            throw new IOException("Path not found");
        }

        SafeFileHandle directoryHandle = CreateFile(path, 0, 2, IntPtr.Zero, CREATION_DISPOSITION_OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, IntPtr.Zero); //Handle file / folder

        if (directoryHandle.IsInvalid)
        {
            throw new Win32Exception(Marshal.GetLastWin32Error());
        }

        StringBuilder result = new StringBuilder(512);
        int mResult = GetFinalPathNameByHandle(directoryHandle, result, result.Capacity, 0);

        if (mResult < 0)
        {
            throw new Win32Exception(Marshal.GetLastWin32Error());
        }

        if (result.Length >= 4 && result[0] == '\\' && result[1] == '\\' && result[2] == '?' && result[3] == '\\')
        {
            return result.ToString().Substring(4); // "\\?\" remove
        }
        return result.ToString();
     }
like image 121
Emil Eremiev Avatar answered Nov 06 '22 06:11

Emil Eremiev