none
Symbolic Links RRS feed

  • Question

  • using VB.net

    I have symbolic link file in a folder; E:\folderName\File.pdf  where File.pdf is a symbolic link for E:\SomeOtherFolderName\SubFolder\File.pdf

    I am trying to programmatically get the original path of File.pdf, which should return: 

    E:\SomeOtherFolderName\SubFolder\File.pdf

    Thank you


    GADOI

    Tuesday, February 12, 2019 2:12 PM

Answers

  • A test with  GetFinalPathNameByHandle works : (test on a .jpg)

    Dim hFile As IntPtr = CreateFile("e:\toto1\test.jpg", GENERIC_READ, FILE_SHARE_READ Or FILE_SHARE_WRITE,
                    IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero)
    If (hFile <> CType(-1, IntPtr)) Then
        Dim sFilePath As StringBuilder = New StringBuilder(32767)
    
        GetFinalPathNameByHandle(hFile, sFilePath, sFilePath.Capacity, 0)
        CloseHandle(hFile)
    End If

    Declarations :

     <DllImport("Kernel32.dll", SetLastError:=True, CharSet:=CharSet.Unicode)>
        Friend Shared Function CreateFile(ByVal lpFileName As String, ByVal dwDesiredAccess As Integer, ByVal dwShareMode As Integer, ByVal lpSECURITY_ATTRIBUTES As IntPtr, ByVal dwCreationDisposition As Integer, ByVal dwFlagsAndAttributes As Integer, ByVal hTemplateFile As IntPtr) As IntPtr
        End Function
        
        <DllImport("Kernel32.dll", SetLastError:=True, CharSet:=CharSet.Unicode)>
        Friend Shared Function CloseHandle(hObject As IntPtr) As Boolean
        End Function
        
        Public Const GENERIC_READ As Integer = &H80000000
        Public Const ERROR_INSUFFICIENT_BUFFER As Integer = 122
    
        Public Const FILE_SHARE_READ As Integer = 1
        Public Const FILE_SHARE_WRITE As Integer = 2
        Public Const FILE_SHARE_DELETE As Integer = 4
        Public Const FILE_ATTRIBUTE_NORMAL As Integer = &H80
    
        Public Const CREATE_NEW As Integer = 1
        Public Const CREATE_ALWAYS As Integer = 2
        Public Const OPEN_EXISTING As Integer = 3
        Public Const OPEN_ALWAYS As Integer = 4
        Public Const TRUNCATE_EXISTING As Integer = 5



    • Marked as answer by GAINSCOM Friday, March 1, 2019 5:54 PM
    Friday, February 15, 2019 12:09 PM

All replies

  • For example with GetFinalPathNameByHandle

    P/Invoke :

       <DllImport("Kernel32.dll", SetLastError:=True, CharSet:=CharSet.Unicode)>
        Public Shared Function GetFinalPathNameByHandle(hFile As IntPtr, lpszFilePath As StringBuilder, cchFilePath As Integer, dwFlags As Integer) As Integer
        End Function


    • Edited by Castorix31 Tuesday, February 12, 2019 2:50 PM
    Tuesday, February 12, 2019 2:35 PM
  • Hi,

    You can learn about iTextSharp dll.

    1.First download the component and add a reference, here is to use the VS comes with NuGet to install, key project selection management NuGet package, search iTextSharp select the appropriate version to install, the installation will automatically add a reference

    2.Introducing a namespace,call function.

    Imports iTextSharp.text
      Dim reader As PdfReader = New PdfReader("E:\SomeOtherFolderName\SubFolder\File.pdf")
            Dim list As List(Of PdfAnnotation.PdfImportedLink) = reader.GetLinks(36)

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    • Edited by Alex Li-MSFT Wednesday, February 13, 2019 3:49 AM
    Wednesday, February 13, 2019 3:48 AM
  • Nice approach - and there are other benefits to using this dll too.

    Thank you.

    However, the pdf was just an example.  It could be any file format, however, currently, 99% are pdfs....

    Interesting that this is so difficult specially with the richness of .net.    have to look at pinvoke, etc...

    Thanks


    GADOI

    Wednesday, February 13, 2019 11:45 AM
  • Hi,

    Do you resolve the issue or are you still having any problem?

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, February 15, 2019 2:54 AM
  • Not resolved - my current approach is to find the symlink file specifications so that I can retrieve the path from the file itself.

    I would much prefer not using yet another dll and then not a third party dll.

    There has to be some .net approach.  Worked with the pinvoke some but it is not my area of expertise and I read about calling Windows API (which I usually do not do, so a lot of catching up to do if i go that route)

    Sorry - no good news...


    GADOI

    Friday, February 15, 2019 11:53 AM
  • A test with  GetFinalPathNameByHandle works : (test on a .jpg)

    Dim hFile As IntPtr = CreateFile("e:\toto1\test.jpg", GENERIC_READ, FILE_SHARE_READ Or FILE_SHARE_WRITE,
                    IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero)
    If (hFile <> CType(-1, IntPtr)) Then
        Dim sFilePath As StringBuilder = New StringBuilder(32767)
    
        GetFinalPathNameByHandle(hFile, sFilePath, sFilePath.Capacity, 0)
        CloseHandle(hFile)
    End If

    Declarations :

     <DllImport("Kernel32.dll", SetLastError:=True, CharSet:=CharSet.Unicode)>
        Friend Shared Function CreateFile(ByVal lpFileName As String, ByVal dwDesiredAccess As Integer, ByVal dwShareMode As Integer, ByVal lpSECURITY_ATTRIBUTES As IntPtr, ByVal dwCreationDisposition As Integer, ByVal dwFlagsAndAttributes As Integer, ByVal hTemplateFile As IntPtr) As IntPtr
        End Function
        
        <DllImport("Kernel32.dll", SetLastError:=True, CharSet:=CharSet.Unicode)>
        Friend Shared Function CloseHandle(hObject As IntPtr) As Boolean
        End Function
        
        Public Const GENERIC_READ As Integer = &H80000000
        Public Const ERROR_INSUFFICIENT_BUFFER As Integer = 122
    
        Public Const FILE_SHARE_READ As Integer = 1
        Public Const FILE_SHARE_WRITE As Integer = 2
        Public Const FILE_SHARE_DELETE As Integer = 4
        Public Const FILE_ATTRIBUTE_NORMAL As Integer = &H80
    
        Public Const CREATE_NEW As Integer = 1
        Public Const CREATE_ALWAYS As Integer = 2
        Public Const OPEN_EXISTING As Integer = 3
        Public Const OPEN_ALWAYS As Integer = 4
        Public Const TRUNCATE_EXISTING As Integer = 5



    • Marked as answer by GAINSCOM Friday, March 1, 2019 5:54 PM
    Friday, February 15, 2019 12:09 PM
  • You should be employed!

    Thank you VERY much!


    GADOI

    Friday, March 1, 2019 5:55 PM