Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 获取lnk文件的完整路径_Vb.net_Visual Studio_Text_Text Files_Streamwriter - Fatal编程技术网

Vb.net 获取lnk文件的完整路径

Vb.net 获取lnk文件的完整路径,vb.net,visual-studio,text,text-files,streamwriter,Vb.net,Visual Studio,Text,Text Files,Streamwriter,我有一个文本文件,内容如下: F:\-=Temporal=-\Documentos\DVD Temazos\Rock\Breaking Benjamin - Fade Away.mp3.lnk F:\-=Temporal=-\Documentos\DVD Temazos\Rock\Breaking Benjamin - Lights Out.mp3.lnk F:\-=Temporal=-\Documentos\DVD Temazos\Rock\Versaemerge - No Consequen

我有一个文本文件,内容如下:

F:\-=Temporal=-\Documentos\DVD Temazos\Rock\Breaking Benjamin - Fade Away.mp3.lnk
F:\-=Temporal=-\Documentos\DVD Temazos\Rock\Breaking Benjamin - Lights Out.mp3.lnk
F:\-=Temporal=-\Documentos\DVD Temazos\Rock\Versaemerge - No Consequences.mp3.lnk
F:\-=Temporal=-\Documentos\DVD Temazos\Rock\VersaEmerge - Past Praying for.mp3.lnk
F:\-=Temporal=-\Documentos\DVD Temazos\Rock\Watch Commander - Clock & Compass.mp3.lnk
我需要的是获取链接文件的所有实际路径,并将其存储在var中

例如,如果文本文件包含以下行:

F:\-=Temporal=-\Documentos\DVD Temazos\Rock\VersaEmerge - Let Down.mp3.lnk
然后我需要得到真实的位置,这是:

F:\Music\Songs\2010\Rock (English)\VersaEmerge - Let Down.mp3
我使用下一种方法获取文件夹的所有“.lnk”文件,然后将文件名写入文本文件(我上面提到的文本文件)

PS:我发布这篇文章是因为可能有人知道如何在这里为我做一个小把戏:

   Dim Temp_file As String = System.IO.Path.GetTempPath & "\PlayList_temp.m3u"

    Using objWriter As New System.IO.StreamWriter(Temp_file, False)
        For Each folder In ArgsArray
            If Not folder = Nothing Then
                Dim di As New IO.DirectoryInfo(folder)
                Dim files As IO.FileInfo() = di.GetFiles("*.lnk")
                Dim file As IO.FileInfo
                For Each file In files
                    objWriter.Write(file.FullName & vbCrLf)
                Next
            End If
        Next
        End Using
我测试了这个用C#编写的解决方案。以下是用VB.NET编写的解决方案:

Imports System.Runtime.InteropServices
Imports System.Text

Public Class Form1

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Dim TargetFilename As String = ResolveShortcut("C:\Users\WinUser\Desktop\Test.pdf - Shortcut.lnk")
    End Sub

    <DllImport("shfolder.dll", CharSet:=CharSet.Auto)>
    Friend Shared Function SHGetFolderPath(hwndOwner As IntPtr, nFolder As Integer, hToken As IntPtr, dwFlags As Integer, lpszPath As StringBuilder) As Integer
    End Function

    <Flags()>
    Private Enum SLGP_FLAGS
        ''' <summary>Retrieves the standard short (8.3 format) file name</summary>
        SLGP_SHORTPATH = &H1
        ''' <summary>Retrieves the Universal Naming Convention (UNC) path name of the file</summary>
        SLGP_UNCPRIORITY = &H2
        ''' <summary>Retrieves the raw path name. A raw path is something that might not exist and may include environment variables that need to be expanded</summary>
        SLGP_RAWPATH = &H4
    End Enum

    <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Auto)>
    Private Structure WIN32_FIND_DATAW
        Public dwFileAttributes As UInteger
        Public ftCreationTime As Long
        Public ftLastAccessTime As Long
        Public ftLastWriteTime As Long
        Public nFileSizeHigh As UInteger
        Public nFileSizeLow As UInteger
        Public dwReserved0 As UInteger
        Public dwReserved1 As UInteger
        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=260)>
        Public cFileName As String
        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=14)>
        Public cAlternateFileName As String
    End Structure

    <Flags()>
    Private Enum SLR_FLAGS
        ''' <summary>
        ''' Do not display a dialog box if the link cannot be resolved. When SLR_NO_UI is set,
        ''' the high-order word of fFlags can be set to a time-out value that specifies the
        ''' maximum amount of time to be spent resolving the link. The function returns if the
        ''' link cannot be resolved within the time-out duration. If the high-order word is set
        ''' to zero, the time-out duration will be set to the default value of 3,000 milliseconds
        ''' (3 seconds). To specify a value, set the high word of fFlags to the desired time-out
        ''' duration, in milliseconds.
        ''' </summary>
        SLR_NO_UI = &H1
        ''' <summary>Obsolete and no longer used</summary>
        SLR_ANY_MATCH = &H2
        ''' <summary>If the link object has changed, update its path and list of identifiers.
        ''' If SLR_UPDATE is set, you do not need to call IPersistFile::IsDirty to determine
        ''' whether or not the link object has changed.</summary>
        SLR_UPDATE = &H4
        ''' <summary>Do not update the link information</summary>
        SLR_NOUPDATE = &H8
        ''' <summary>Do not execute the search heuristics</summary>
        SLR_NOSEARCH = &H10
        ''' <summary>Do not use distributed link tracking</summary>
        SLR_NOTRACK = &H20
        ''' <summary>Disable distributed link tracking. By default, distributed link tracking tracks
        ''' removable media across multiple devices based on the volume name. It also uses the
        ''' Universal Naming Convention (UNC) path to track remote file systems whose drive letter
        ''' has changed. Setting SLR_NOLINKINFO disables both types of tracking.</summary>
        SLR_NOLINKINFO = &H40
        ''' <summary>Call the Microsoft Windows Installer</summary>
        SLR_INVOKE_MSI = &H80
    End Enum

    ''' <summary>The IShellLink interface allows Shell links to be created, modified, and resolved</summary>
    <ComImport(), InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid("000214F9-0000-0000-C000-000000000046")>
    Private Interface IShellLinkW
        ''' <summary>Retrieves the path and file name of a Shell link object</summary>
        Sub GetPath(<Out(), MarshalAs(UnmanagedType.LPWStr)> pszFile As StringBuilder, cchMaxPath As Integer, ByRef pfd As WIN32_FIND_DATAW, fFlags As SLGP_FLAGS)
        ''' <summary>Retrieves the list of item identifiers for a Shell link object</summary>
        Sub GetIDList(ByRef ppidl As IntPtr)
        ''' <summary>Sets the pointer to an item identifier list (PIDL) for a Shell link object.</summary>
        Sub SetIDList(pidl As IntPtr)
        ''' <summary>Retrieves the description string for a Shell link object</summary>
        Sub GetDescription(<Out(), MarshalAs(UnmanagedType.LPWStr)> pszName As StringBuilder, cchMaxName As Integer)
        ''' <summary>Sets the description for a Shell link object. The description can be any application-defined string</summary>
        Sub SetDescription(<MarshalAs(UnmanagedType.LPWStr)> pszName As String)
        ''' <summary>Retrieves the name of the working directory for a Shell link object</summary>
        Sub GetWorkingDirectory(<Out(), MarshalAs(UnmanagedType.LPWStr)> pszDir As StringBuilder, cchMaxPath As Integer)
        ''' <summary>Sets the name of the working directory for a Shell link object</summary>
        Sub SetWorkingDirectory(<MarshalAs(UnmanagedType.LPWStr)> pszDir As String)
        ''' <summary>Retrieves the command-line arguments associated with a Shell link object</summary>
        Sub GetArguments(<Out(), MarshalAs(UnmanagedType.LPWStr)> pszArgs As StringBuilder, cchMaxPath As Integer)
        ''' <summary>Sets the command-line arguments for a Shell link object</summary>
        Sub SetArguments(<MarshalAs(UnmanagedType.LPWStr)> pszArgs As String)
        ''' <summary>Retrieves the hot key for a Shell link object</summary>
        Sub GetHotkey(ByRef pwHotkey As Short)
        ''' <summary>Sets a hot key for a Shell link object</summary>
        Sub SetHotkey(wHotkey As Short)
        ''' <summary>Retrieves the show command for a Shell link object</summary>
        Sub GetShowCmd(ByRef piShowCmd As Integer)
        ''' <summary>Sets the show command for a Shell link object. The show command sets the initial show state of the window.</summary>
        Sub SetShowCmd(iShowCmd As Integer)
        ''' <summary>Retrieves the location (path and index) of the icon for a Shell link object</summary>
        Sub GetIconLocation(<Out(), MarshalAs(UnmanagedType.LPWStr)> pszIconPath As StringBuilder, cchIconPath As Integer, ByRef piIcon As Integer)
        ''' <summary>Sets the location (path and index) of the icon for a Shell link object</summary>
        Sub SetIconLocation(<MarshalAs(UnmanagedType.LPWStr)> pszIconPath As String, iIcon As Integer)
        ''' <summary>Sets the relative path to the Shell link object</summary>
        Sub SetRelativePath(<MarshalAs(UnmanagedType.LPWStr)> pszPathRel As String, dwReserved As Integer)
        ''' <summary>Attempts to find the target of a Shell link, even if it has been moved or renamed</summary>
        Sub Resolve(hwnd As IntPtr, fFlags As SLR_FLAGS)
        ''' <summary>Sets the path and file name of a Shell link object</summary>
        Sub SetPath(<MarshalAs(UnmanagedType.LPWStr)> pszFile As String)

    End Interface

    <ComImport(), Guid("0000010c-0000-0000-c000-000000000046"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)>
    Public Interface IPersist
        <PreserveSig()>
        Sub GetClassID(ByRef pClassID As Guid)
    End Interface


    <ComImport(), Guid("0000010b-0000-0000-C000-000000000046"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)>
    Public Interface IPersistFile
        Inherits IPersist
        Shadows Sub GetClassID(ByRef pClassID As Guid)
        <PreserveSig()>
        Function IsDirty() As Integer

        <PreserveSig()>
        Sub Load(<[In](), MarshalAs(UnmanagedType.LPWStr)> pszFileName As String, dwMode As UInteger)

        <PreserveSig()>
        Sub Save(<[In](), MarshalAs(UnmanagedType.LPWStr)> pszFileName As String, <[In](), MarshalAs(UnmanagedType.Bool)> fRemember As Boolean)

        <PreserveSig()>
        Sub SaveCompleted(<[In](), MarshalAs(UnmanagedType.LPWStr)> pszFileName As String)

        <PreserveSig()>
        Sub GetCurFile(<[In](), MarshalAs(UnmanagedType.LPWStr)> ppszFileName As String)
    End Interface

    Const STGM_READ As UInteger = 0
    Const MAX_PATH As Integer = 260

    ' CLSID_ShellLink from ShlGuid.h 
    <ComImport(), Guid("00021401-0000-0000-C000-000000000046")> Public Class ShellLink
    End Class


    Public Shared Function ResolveShortcut(filename As String) As String
        Dim link As New ShellLink()
        DirectCast(link, IPersistFile).Load(filename, STGM_READ)
        ' TODO: if I can get hold of the hwnd call resolve first. This handles moved and renamed files.  
        ' ((IShellLinkW)link).Resolve(hwnd, 0) 
        Dim sb As New StringBuilder(MAX_PATH)
        Dim data As New WIN32_FIND_DATAW()
        DirectCast(link, IShellLinkW).GetPath(sb, sb.Capacity, data, 0)
        Return sb.ToString()
    End Function

End Class
导入System.Runtime.InteropServices
导入系统文本
公开课表格1
私有子表单1_Load(发送方作为System.Object,e作为System.EventArgs)处理MyBase.Load
Dim TargetFilename为String=ResolveShortcut(“C:\Users\WinUser\Desktop\Test.pdf-Shortcut.lnk”)
端接头
朋友共享函数SHGetFolderPath(hwndOwner为IntPtr,nFolder为Integer,hToken为IntPtr,dwFlags为Integer,lpszPath为StringBuilder)为整数
端函数
私有枚举SLGP_标志
''检索标准短(8.3格式)文件名
SLGP_短路径=&H1
''检索文件的通用命名约定(UNC)路径名
SLGP_UNCPRIORITY=&H2
''检索原始路径名。原始路径可能不存在,并且可能包含需要扩展的环境变量
SLGP_RAWPATH=&H4
结束枚举
私有结构WIN32_FIND_DATAW
作为UInteger的公共dwFileAttributes
公共ftCreationTime尽可能长
公共ftLastAccessTime尽可能长
公共ftLastWriteTime尽可能长
公共nFileSizeHigh As UInteger
公共信息披露(以下简称为UInteger)
公共数据保留为UInteger
公共数据仓库1保留为UInteger
公共cFileName作为字符串
公共cAlternateFileName作为字符串
端部结构
私有枚举SLR_标志
''' 
''如果无法解析链接,则不显示对话框。当设置SLR\u NO\u UI时,
''fFlags的高阶字可以设置为一个超时值,该值指定
''解析链接所花费的最大时间量。如果
无法在超时持续时间内解析“”链接。如果设置了高阶字
''为零,超时持续时间将设置为默认值3000毫秒
''(3秒)。要指定值,请将fFlags的高位字设置为所需的超时
''持续时间,以毫秒为单位。
''' 
单反编号用户界面=&H1
''已过时且不再使用
单反任意匹配=&H2
''如果链接对象已更改,请更新其路径和标识符列表。
''如果设置了SLR_更新,则无需调用IPersistFile::IsDirty来确定
''链接对象是否已更改。
SLR_更新=&H4
''不更新链接信息
单反更新=&H8
''不执行搜索启发式
SLR_鼻弓=&H10
''不使用分布式链接跟踪
SLR\U NOTRACK=&H20
''禁用分布式链接跟踪。默认情况下,分布式链接跟踪跟踪
''基于卷名跨多个设备的可移动媒体。它还使用
''通用命名约定(UNC)路径,用于跟踪驱动器号为
''已经改变了。设置SLR\u NOLINKINFO将禁用这两种类型的跟踪。
SLR_NOLINKINFO=&H40
''请致电Microsoft Windows安装程序
SLR_INVOKE_MSI=&H80
结束枚举
''IShellLink接口允许创建、修改和解析Shell链接
专用接口IShellLinkW
''检索Shell链接对象的路径和文件名
子GetPath(pszFile作为StringBuilder,cchMaxPath作为整数,ByRef pfd作为WIN32_FIND_DATAW,fFlags作为SLGP_标志)
''检索Shell链接对象的项标识符列表
子GetIDList(ByRef ppidl作为IntPtr)
''为Shell链接对象设置指向项标识符列表(PIDL)的指针。
子集合IDList(pidl作为IntPtr)
''检索Shell链接对象的描述字符串
子GetDescription(pszName作为StringBuilder,cchMaxName作为整数)
''设置外壳链接对象的描述。描述可以是任何应用程序定义的字符串
子集合描述(pszName作为字符串)
''检索Shell链接对象的工作目录的名称
子GetWorkingDirectory(pszDir作为StringBuilder,cchMaxPath作为整数)
''为Shell链接对象设置工作目录的名称
子集合工作目录(pszDir作为字符串)
''检索与Shell链接对象关联的命令行参数
子GetArguments(pszArgs作为StringBuilder,cchMaxPath作为整数)
''设置Shell链接对象的命令行参数
子集合参数(pszArgs作为字符串)
''检索外壳链接对象的热键
子GetHotkey(ByRef pwHotkey简称)
''为Shell链接对象设置热键
子设置热键(wHotkey为短键)
''检索Shell链接对象的show命令
子GetShowCmd(ByRef piShowCmd作为整数)
''为Shell链接对象设置显示命令。show命令设置窗口的初始显示状态。
子SetShowCmd(iShowCmd为整数)
''检索外壳链接对象的图标位置(路径和索引)
子GetIconLocation(pszIconPath作为StringBuilder,cchIconPath作为整数,ByRef piIcon作为整数)
''为Shell链接对象设置图标的位置(路径和索引)
子集合IConLocation(pszIconPath作为字符串,iIc