此代码可以让你在系统的资源管理器中定位一个文件或者是文件夹!

使用方法很简单
' 如果已经打开了目标文件的文件夹,此方法并不会再打开一个新的explorer管理器,而是在原有的资源管理器内直接定位文件!
SelectTargetFile.SelectFile("指定文件或者文件夹")

' 这个方法可以直接打开文件夹内部,而不是定位到该文件夹的父目录
SelectTargetFile.SelectFile("指定文件夹\一个不存在的文件.xxx")
完整代码如下:

Imports System.Runtime.InteropServices
Imports System.Text

Public NotInheritable Class SelectTargetFile
    Private Sub New()
    End Sub



    ''' <summary>在Explorer中定位文件或文件夹</summary>
    Shared Sub SelectFile(ByVal filePath As String)
        Dim ppsl As IntPtr = IntPtr.Zero
        Dim result As Integer = CoCreateInstance(CLSID_ShellLink, IntPtr.Zero, CLSCTX.INPROC_SERVER, IID_IShellLink, ppsl)
        Dim psl As IShellLinkW = Marshal.GetObjectForIUnknown(ppsl)
        psl.SetPath(filePath)
        Dim pidl As IntPtr = IntPtr.Zero
        psl.GetIDList(pidl)
        SHOpenFolderAndSelectItems(pidl, 0, Nothing, 0)
        Marshal.FreeCoTaskMem(pidl)
        Marshal.Release(ppsl)
    End Sub



    Private Shared CLSID_ShellLink = New Guid("00021401-0000-0000-C000-000000000046")
    Private Shared IID_IShellLink = New Guid("000214F9-0000-0000-C000-000000000046")

    Private Declare Function SHOpenFolderAndSelectItems Lib "shell32.dll" (ByVal pidlFolder As IntPtr, ByVal cidl As UInteger, <[In](), MarshalAs(UnmanagedType.LPArray)> ByVal apidll As IntPtr(), ByVal dwFlags As UInteger) As Integer
    Private Declare Function CoCreateInstance Lib "ole32.dll" (<[In]()> ByRef rclsid As Guid, ByVal pUnkOuter As IntPtr, ByVal dwClsContext As CLSCTX, <[In]()> ByRef riid As Guid, <Out()> ByRef ppv As IntPtr) As Integer

    Public Enum CLSCTX As UInteger
        INPROC_SERVER = &H1
    End Enum

    <Serializable(), StructLayout(LayoutKind.Sequential)> _
    Public Structure FILETIME
        Public dwLowDateTime As UInteger
        Public dwHighDateTime As UInteger
    End Structure

    <Serializable(), StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Unicode), BestFitMapping(False)> _
    Public Structure WIN32_FIND_DATAW
        Public dwFileAttributes As UInteger
        Public ftCreationTime As FILETIME
        Public ftLastAccessTime As FILETIME
        Public ftLastWriteTime As FILETIME
        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

    <ComImport(), InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid("000214F9-0000-0000-C000-000000000046")> _
    Public Interface IShellLinkW
        <PreserveSig()> Function GetPath(ByVal pszFile As StringBuilder, ByVal cch As Integer, <[In](), Out()> ByRef pfd As WIN32_FIND_DATAW, ByVal fFlags As UInteger) As Integer
        <PreserveSig()> Function GetIDList(<Out()> ByRef ppidl As IntPtr) As Integer
        <PreserveSig()> Function SetIDList(<[In]()> ByRef pidl As IntPtr) As Integer
        <PreserveSig()> Function GetDescription(ByVal pszName As StringBuilder, ByVal cch As Integer) As Integer
        <PreserveSig()> Function SetDescription(<MarshalAs(UnmanagedType.LPWStr)> ByVal pszName As String) As Integer
        <PreserveSig()> Function GetWorkingDirectory(ByVal pszDir As StringBuilder, ByVal cch As Integer) As Integer
        <PreserveSig()> Function SetWorkingDirectory(<MarshalAs(UnmanagedType.LPWStr)> ByVal pszDir As String) As Integer
        <PreserveSig()> Function GetArguments(ByVal pszArgs As StringBuilder, ByVal cch As Integer) As Integer
        <PreserveSig()> Function SetArguments(<MarshalAs(UnmanagedType.LPWStr)> ByVal pszArgs As String) As Integer
        <PreserveSig()> Function GetHotkey(<Out()> ByRef pwHotkey As UShort) As Integer
        <PreserveSig()> Function SetHotkey(ByVal wHotkey As UShort) As Integer
        <PreserveSig()> Function GetShowCmd(<Out()> ByRef piShowCmd As Integer) As Integer
        <PreserveSig()> Function SetShowCmd(ByVal iShowCmd As Integer) As Integer
        <PreserveSig()> Function GetIconLocation(ByVal pszIconPath As StringBuilder, ByVal cch As Integer, <Out()> ByRef piIcon As Integer) As Integer
        <PreserveSig()> Function SetIconLocation(<MarshalAs(UnmanagedType.LPWStr)> ByVal pszIconPath As String, ByVal iIcon As Integer) As Integer
        <PreserveSig()> Function SetRelativePath(<MarshalAs(UnmanagedType.LPWStr)> ByVal pszPathRel As String, ByVal dwReserved As UInteger) As Integer
        <PreserveSig()> Function Resolve(ByVal hwnd As IntPtr, ByVal fFlags As UInteger) As Integer
        <PreserveSig()> Function SetPath(<MarshalAs(UnmanagedType.LPWStr)> ByVal pszFile As String) As Integer
    End Interface

End Class