vb.net的file.shortpath

vb.net的file.shortpath,vb.net,file,vb6,converter,Vb.net,File,Vb6,Converter,我正在将我的项目从vb6转换为vb.net vb.net中是否有一种模拟短路径的方法 Dim DestinationFile As Scripting.File ... DestinationFile.ShortPath 谢谢到目前为止,我能找到的是没有用于此的托管代码,但您可以这样做: Imports System.Text Imports System.Runtime.InteropServices Module Module1 Declare Unicode Function

我正在将我的项目从vb6转换为vb.net vb.net中是否有一种模拟短路径的方法

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

谢谢

到目前为止,我能找到的是没有用于此的托管代码,但您可以这样做:

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
导入系统文本
导入System.Runtime.InteropServices
模块1
将Unicode函数GetShortPathName Lib“kernel32.dll”别名“GetShortPathNameW”(ByVal longPath作为字符串,ByVal ShortPath作为System.Text.StringBuilder,ByVal bufferSize作为整数)声明为整数
副标题()
Dim filespec As String=“SomeLongName”
作为StringBuilder的Dim sbShortPath=新StringBuilder()
GetShortPathName(文件规范,sbShortPath,255)
将短路径设置为字符串=sbShortPath.ToString
端接头
端模块

方法声明位于

否,现代语言不是为使用短路径而构建的,但您可以从内核使用GetShortPathName方法:

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

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

您的VB6正在通过COM使用Windows


在.Net中似乎没有获取短文件名的功能。与在其他答案中使用P/Invoke调用Windows API不同,在VB.Net中通过COM使用相同的FileSystemObject更简单。只需将COM引用添加到“Microsoft脚本运行时”。那么VB6代码在VB.Net中的工作方式可能几乎不变。

您是否依赖此路径?那么禁用短名称的机器呢?这是一行代码吗?因为您要迁移到.NET,所以可以省略它吗?