函数参考


_WinAPI_SetFileShortName

Sets the short name for the specified file.

#Include <WinAPIEx.au3>
_WinAPI_SetFileShortName ( $hFile, $sShortName )

参数

$hFile 一个文件句柄. The file must be on an NTFS file system volume. Also, the file must be
opened with either the $GENERIC_ALL access right or $GENERIC_WRITE|$DELETE, and with the
$FILE_FLAG_BACKUP_SEMANTICS file attribute.
$sShortName The valid short name for the file. If the specified short name already exists, the function
fails and the last error code is ERROR_ALREADY_EXISTS (183).

返回值

成功: 返回 1.
失败: 返回 0 并设置 @error 标志为非 0 值.

注意/说明

The caller of this function must have the $SE_RESTORE_NAME privilege.

相关

详情参考

在MSDN中搜索


示例/演示


#Include <APIConstants.au3>
#Include <WinAPIEx.au3>

Opt('MustDeclareVars', 1)

Global Const $sTemp = @TempDir & '\Temporary File.txt'

Global $hToken, $hFile, $aAdjust

; Check NTFS file system
If StringCompare(DriveGetFileSystem(_WinAPI_PathStripToRoot($sTemp)), 'NTFS') Then
    MsgBox(16, 'Error', 'The file must be on an NTFS file system volume.')
    Exit
EndIf

; Enable "SeRestorePrivilege" privilege to perform renaming operation
$hToken = _WinAPI_OpenProcessToken(BitOR($TOKEN_ADJUST_PRIVILEGES, $TOKEN_QUERY))
_WinAPI_AdjustTokenPrivileges($hToken, $SE_RESTORE_NAME, $SE_PRIVILEGE_ENABLED, $aAdjust)
If @error Or @extended Then
    MsgBox(16, 'Error', 'You do not have the required privileges.')
    Exit
EndIf

; Create temporary file
FileWrite($sTemp, '')

ConsoleWrite('Old short name: ' & _WinAPI_PathStripPath(FileGetShortName($sTemp)) & @CR)

; Set "TEMP.TXT" short name for the file
$hFile = _WinAPI_CreateFileEx($sTemp, $OPEN_EXISTING, BitOR($GENERIC_WRITE, $DELETE), 0, $FILE_FLAG_BACKUP_SEMANTICS)
_WinAPI_SetFileShortName($hFile, 'TEMP.TXT')
_WinAPI_CloseHandle($hFile)

ConsoleWrite('New short name: ' & _WinAPI_PathStripPath(FileGetShortName($sTemp)) & @CR)

; Delete temporary file
FileDelete($sTemp)

; Restore "SeRestorePrivilege" privilege by default
_WinAPI_AdjustTokenPrivileges($hToken, $aAdjust, 0, $aAdjust)
_WinAPI_CloseHandle($hToken)