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
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
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()
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
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.