Visual studio 使用Process.Start和SHGetFileInfo()启动某些.lnk文件并获取其图标时出错

Visual studio 使用Process.Start和SHGetFileInfo()启动某些.lnk文件并获取其图标时出错,visual-studio,process,icons,shortcut,win32exception,Visual Studio,Process,Icons,Shortcut,Win32exception,由于某些奇怪和未知的原因,我无法使用以下方法启动某些快捷方式并获取它们的图标: Public Shared Sub Launch(itemToLaunch As String) Process.Start(itemToLaunch) End Sub Public Function GetShellIcon(ByVal path As String) As Icon Dim info As SHFILEINFO = New SHFILEINFO()

由于某些奇怪和未知的原因,我无法使用以下方法启动某些快捷方式并获取它们的图标:

Public Shared Sub Launch(itemToLaunch As String)
        Process.Start(itemToLaunch)
End Sub



Public Function GetShellIcon(ByVal path As String) As Icon

        Dim info As SHFILEINFO = New SHFILEINFO()
        Dim retval As IntPtr = SHGetFileInfo(path, 0, info, Marshal.SizeOf(info), SHGFI_ICON Or SHGFI_SMALLICON Or SHGFI_LARGEICON)

        If retval = IntPtr.Zero Then
            Return New Icon(GetType(Control), "Error.ico")
        End If

        Dim cargt() As Type = {GetType(IntPtr)}
        Dim ci As ConstructorInfo = GetType(Icon).GetConstructor(BindingFlags.NonPublic Or BindingFlags.Instance, Nothing, cargt, Nothing)
        Dim cargs() As Object = {info.IconHandle}
        Dim icon As Icon = CType(ci.Invoke(cargs), Icon)

        Return icon
    End Function

 <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Auto)>
    Private Structure SHFILEINFO
        Public IconHandle As IntPtr
        Public IconIndex As Integer
        Public Attributes As UInteger
        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=260)>
        Public DisplayString As String
        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=80)>
        Public TypeName As String
    End Structure

    Private Declare Auto Function SHGetFileInfo Lib "Shell32.dll" (path As String, attributes As Integer, ByRef info As SHFILEINFO, infoSize As Integer, flags As Integer) As IntPtr

    Public Const SHGFI_ICON = &H100
    Public Const SHGFI_SMALLICON = &H1
    Public Const SHGFI_LARGEICON = &H0         ' Large icon
然后执行

您将获得以下信息:

单击任何显示良好的图标启动WinRar应用程序。
单击错误显示图标显示此错误:

使用错误路径更改
Me.LnkOrigPictureBox.Tag的值,如
“C:\Users\Moi\Desktop\WinRARdontexistshere.exe linkorig.lnk”
,执行相同操作将显示另一个可视错误和错误(如预期的那样):

这与
DVDMaker.exe都不起作用

但是,通过
wordpad.exe
、图标和应用程序启动,一切都很好

(我已经测试了小写/大写的大小写,看看是否相互干扰,但这不是问题所在)

我注意到一些其他应用程序出现了问题,但不了解其原因,例如:

  • 油漆网
  • 虚拟盒
  • 克朗斯比
  • 虚拟图书馆
和其他标准Windows应用程序

将有问题的文件路径
C:\Users\Moi\Desktop\WinRAR.exe linkorig.lnk
复制/粘贴到Windows资源管理器标题栏时,将启动WinRAR.exe应用程序

当然,我双击.lnk文件也是一样的

当复制/粘贴到Windows-R命令窗口时,它也可以很好地启动

如果通过在
C:\Users\Moi\Desktop\
文件夹中的命令行窗口中键入
WinRAR.lnk
调用,也会启动

我运行的是64位的Windows7。该应用程序使用Visual Studio Express 2015编译。我以管理员身份登录(Windows安装中创建的唯一默认帐户)。“以管理员身份”运行已编译的应用程序不会改变任何事情

我尝试使用一些配置,例如以下配置,但没有成功:

        Dim info As ProcessStartInfo = New ProcessStartInfo(--- here the path ---)
        info.CreateNoWindow = False
        info.UseShellExecute = False
        info.RedirectStandardError = True
        info.RedirectStandardOutput = True
        info.RedirectStandardInput = True
        Dim whatever As Process = Process.Start(info)

如何解决启动问题以及这些文件的图标检索问题。。。当我看到使用标准的
OpenFileDialog
时,图标问题和尝试使用相应文件时出现的错误消息也出现了,我通过网络上的一些示例进行了一些测试,幸运地找到了答案。我怀疑.Net框架中有一个bug。而解决方案就在这附近,我仍然不太理解它的深层原因

问题如下:

默认情况下,该项目被定义到项目设置中,以与.Net Framework 4.5一起运行

我将其切换为使用Framework 4运行
运行应用程序:没有更多问题

我将其切换回使用Framework 4.5运行
没问题了

        Dim info As ProcessStartInfo = New ProcessStartInfo(--- here the path ---)
        info.CreateNoWindow = False
        info.UseShellExecute = False
        info.RedirectStandardError = True
        info.RedirectStandardOutput = True
        info.RedirectStandardInput = True
        Dim whatever As Process = Process.Start(info)