当前位置: 代码迷 >> VB Dotnet >> GetShortPathName 使用有关问题
  详细解决方案

GetShortPathName 使用有关问题

热度:261   发布时间:2016-04-25 02:00:10.0
GetShortPathName 使用问题
声明
 Declare Function GetShortPathName Lib "kernel32" Alias "GetShortPathNameA" (ByVal lpszLongPath As String, ByVal lpszShortPath As String, ByVal cchBuffer As Long) As Long
使用
 dim p as string=""
 Dim sbtemp1 As String = p
 Dim n As Long = GetShortPathName(p, sbtemp1, 255)
 p = Left(sbtemp1, n)


调试执行方法前 p的值:C:\Users\huanghaic\Desktop\1111\GenDoUtil\DEComPressTemp\1700_0_TFTM_3_AH_2015081801.zip
执行方法后sbtemp1的值:C:\Users\HUANGH~1\Desktop\1111\GENDOU~1\DECOMP~1\1700_0~1.ZIP 0_TFTM_3_AH_2015081801.zip
执行方法后n的值:816043786301
然后在执行到 Left(sbtemp1, n)时抛出异常:算术运算导致溢出。
这是为什么?我怀疑是返回值n不正确。
请大神亲们帮忙指点
------解决思路----------------------
Imports System.Runtime.InteropServices

Module Module1

    <DllImport("kernel32.dll", CharSet:=CharSet.Auto, SetLastError:=True)> _
    Function GetShortPathName(<[In]()> ByVal path As Char(), <Out()> ByVal shortPathBuffer As Char(), ByVal bufferLength As Integer) As Integer
    End Function

    Sub Main()
        Dim p As String = "C:\Users\huanghaic\Desktop\1111\GenDoUtil\DEComPressTemp\1700_0_TFTM_3_AH_2015081801.zip"
        Dim shortPathBuffer As Char() = New Char(260) {}
        Dim n As Integer = GetShortPathName(p, shortPathBuffer, 260)
        p = Left(shortPathBuffer, n)

        Console.WriteLine(n)
        Console.WriteLine(p)
        Console.ReadLine()
    End Sub

End Module
  相关解决方案