Have you had a look at using the GetFinalPathNameByHandle API?
How to get the target folder of a link?
Within my app I let the user open file system directories in explorer. But if I have e.g. a directory
C:\Users\Heiko\Documents\Eigene Bilder\
C:\Users\Heiko\Documents\My Pictures\
and try to open it via
Process.Start(@"C:\Users\Heiko\Documents\Eigene Bilder");
the explorer opens and displays the message 'Cannot open the folder. Access is forbidden.'. Of course the folder is a link to folder 'C:\Users\Heiko\Pictures' and here I have all rights to do something.
How can I get the target path for such folders? I only need the path for opening it via explorer.
Until now I have the following in C#:
WIN32_FIND_DATA wfd = new WIN32_FIND_DATA();
using (SafeFindHandle fh = FindFirstFile(Path.Combine(path, "*"), wfd))
{
if (fh.IsInvalid)
continue;
do
{
if ((wfd.dwFileAttributes & FileAttributes.Directory) == FileAttributes.Directory)
{
if (wfd.cFileName == "." || wfd.cFileName == "..")
continue;
if ((wfd.dwFileAttributes & FileAttributes.ReparsePoint) == FileAttributes.ReparsePoint &&
(wfd.dwReparseTags == IO_REPARSE_TAG_MOUNT_POINT || wfd.dwReparseTags == IO_REPARSE_TAG_SYMLINK))
{
// How to get here the target folder?
}
}
//...
} while (FindNextFile(fh, wfd));
}
2 additional answers
Sort by: Most helpful
-
Xiaopo Yang - MSFT 12,726 Reputation points Microsoft External Staff
2023-02-13T02:45:14.51+00:00 Hello,
Welcome to Microsoft Q&A!
As the answer said, you can use PKEY_Link_TargetParsingPath to retrieve the target path.
Or you can use CreateFile to open the target file and then retrieve the path name.
HANDLE h =CreateFile(L"C:\\Users\\SymbolicLink\\To\\MyFolder", GENERIC_READ, FILE_SHARE_READ,NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS,NULL);
Thank you.
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
-
Heiko 1,291 Reputation points
2023-02-13T12:48:52.9333333+00:00 Thank you, but If I try to get the directory handle by
HANDLE hFile = CreateFileW(L"C:\\Users\\Heiko\\Documents\\Eigene Bilder", GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
I always get 0xFFFFFFFF and last error is 5 (Access denied).
I figured out, that the following works:
HANDLE hFile = CreateFileW(L"C:\\Users\\Heiko\\Documents\\Eigene Bilder", 0, 0, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
Then I can get the linked path with
GetFinalPathNameByHandleW()
.