file.shortpath for vb.net

混江龙づ霸主 提交于 2019-12-13 14:41:20

问题


ho i'm converting a my project from vb6 to vb.net Is there an analog method in vb.net of shortpath?

Dim DestinationFile As Scripting.File
...
DestinationFile.ShortPath

Thanks


回答1:


No, modern languages are not built to use short paths, but you can use the GetShortPathName method from the kernel:

Declare Auto Function GetShortPathName Lib "kernel32.dll" _
(ByVal lpszLongPath As String, ByVal lpszShortPath As StringBuilder, _
ByVal cchBuffer As Integer) As Integer

Call using:

Dim name As String = "C:\Long Directory Name\Really really long filename.txt"

Dim sb As New StringBuilder(256)
GetShortPathName(name, sb, sb.capacity)

Dim shortName As String = sb.ToString()



回答2:


What I could find so far is that there is no managed code for this, but you can do this:

Imports System.Text
Imports System.Runtime.InteropServices

Module Module1

    Declare Unicode Function GetShortPathName Lib "kernel32.dll" Alias "GetShortPathNameW" (ByVal longPath As String, <MarshalAs(UnmanagedType.LPTStr)> ByVal ShortPath As System.Text.StringBuilder, <MarshalAs(UnmanagedType.U4)> ByVal bufferSize As Integer) As Integer

    Sub Main()
        Dim filespec As String = "SomeLongName"

        Dim sbShortPath As StringBuilder = New StringBuilder()
        GetShortPathName(filespec, sbShortPath, 255)

        Dim shortPath As String = sbShortPath.ToString
    End Sub

End Module

Method declaration was found at PInvoke.net




回答3:


Your VB6 is using the Windows FileSystemObject via COM.

It seems there's no functionality in .Net to get a short filename. Rather than using P/Invoke to call the Windows API, as in the other answers, it's simpler to use the same FileSystemObject through COM in your VB.Net. Just add a COM reference to "Microsoft Scripting Runtime". Then the VB6 code will probably work almost unchanged in VB.Net.



来源:https://stackoverflow.com/questions/12727806/file-shortpath-for-vb-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!