GetShortPathName()在XP SP3上无法正常工作
http://msdn.microsoft.com/en-us/library/aa364989(VS.85).aspx
返回路径的输入字符串,如:
C:\Test\LongFolderNameToTestWith\BinarySearch.ini
完全如发送?
然而:
C:\Documents and Settings\LocalService\NTUSER.DAT
是否为路径设置了短名称,因此我知道我正在调用API.
然而:
C:\Documents and Settings\LocalService\BinarySearch.ini
不会从文件名中创建一个简短的名称,但会为路径创建短名称!?
有人可以帮我理解这种行为,也许可以建议一个解决方法.
添加:
我需要能够将8.3路径/文件名传递给遗留应用程序
如何才能做到这一点?
补充:解决方案
经过多次阅读/实验后,似乎唯一可行的方法是使用自动化:
' ------------------------------------------------------------ ' Library Name: Microsoft Scripting Runtime 1.0 ' Library File: C:\WINDOWS\system32\scrrun.dll ' ------------------------------------------------------------ ' Version Info: ' ------------- ' Company Name: Microsoft Corporation ' File Description: Microsoft (R) Script Runtime ' File Version: 5.7.0.16599 ' Internal Name: scrrun.dll ' Legal Copyright: Copyright (C) Microsoft Corp. 1996-2006, All Rights Reserved ' Original Filename: scrrun.dll ' Product Name: Microsoft (R) Script Runtime ' Product Version: 5.7.0.16599 ' ------------------------------------------------------------ ' ProgID: Scripting.FileSystemObject ' Interface Name: ScriptingFileSystemObject ' ' Interface Prefix: Scripting
这有效.
BASIC中的一个简单实现是:
$PROGID_ScriptingFileSystemObject = "Scripting.FileSystemObject" Interface Dispatch ScriptingFileSystemObject Member CALL GetFile <&H0000271C>(IN FilePath AS STRING<&H00000000>) AS ScriptingIFile Member CALL GetFolder<&H0000271D>(IN FolderPath AS STRING<&H00000000>) AS ScriptingIFolder END Interface Interface Dispatch ScriptingFile Member GET ShortPath<&H000003EA>() AS STRING Member GET ShortName<&H000003E9>() AS STRING END Interface Interface Dispatch ScriptingFolder Member GET ShortPath<&H000003EA>() AS STRING Member GET ShortName<&H000003E9>() AS STRING END Interface '----------------------------------------------------------------------------- FUNCTION FileShortPath( BYVAL sPathnFile AS STRING, sShort AS STRING ) AS LONG LOCAL vResult, vFilePath AS Variant LOCAL fso AS ScriptingFileSystemObject LOCAL oFile AS ScriptingFile IF LEN(sPathnFile) = 0 THEN EXIT FUNCTION ' Nothing sent SET fso = NEW ScriptingFileSystemObject IN $PROGID_ScriptingFileSystemObject IF IsNothing(fso) THEN FUNCTION = -1 : EXIT FUNCTION SET oFile = NEW ScriptingFile IN $PROGID_ScriptingFileSystemObject IF IsNothing(oFile) THEN FUNCTION = -2 : EXIT FUNCTION vFilePath = sPathnFile vResult = Empty OBJECT CALL fso.GetFile(vFilePath) TO vResult SET oFile = vResult IF IsNothing(oFile) THEN FUNCTION = -3 : EXIT FUNCTION vResult = Empty Object GET oFile.ShortName TO vResult sShort = VARIANT$(vResult) vResult = Empty Object GET oFile.ShortPath TO vResult sShort = VARIANT$(vResult) IF LEN(sShort) THEN FUNCTION = 1 ' Success END FUNCTION
谢谢大家的建议.
我仍在尝试找到一种可靠地制作8.3路径/文件名的方法.
除了使用GETSHORTPATHNAME之外,还有什么方法可以做到这一点吗?
解决了.往上看
似乎MS继续支持这种仅用于COM的解码......为什么它在C API中现在不可靠仍然是一个谜.
这是因为文件名没有现有的短名称,XP SP3不会自动为该文件创建短名称.
您可以检查此注册表设置(如果存在)以查看当前设置的内容.
HKEY_LOCAL_MACHINE \系统\ CurrentControlSet \控制\ FileSystem的\找到NtfsDisable8dot3NameCreation
当NtfsDisable8dot3NameCreation设置为1时,您将获得以下行为:
如果文件夹/文件已有短名称,例如"Program Files",则它将返回该文件夹/文件的短名称.但是如果不存在短名称,您将获得该文件的长名称,因为它是该对象唯一存在的名称.如果短名称被禁用,那么就没有短名称.