111,094
社区成员




using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
namespace ModifyShortFilename
{
class Program
{
//
// Access Token constants: OpenProcessToken.DesiredAccess
//
public const UInt32 TOKEN_ADJUST_PRIVILEGES = 0x0020;
//
// Token Privileges constants
//
public const string SE_RESTORE_NAME = "SeRestorePrivilege";
//
// Token Privileges constants
//
public const UInt32 SE_PRIVILEGE_ENABLED = 0x00000002;
/// <summary>
///
/// </summary>
public const Int32 ANYSIZE_ARRAY = 1;
[Flags]
public enum EFileAccess : uint
{
GenericAll = 0x10000000,
GenericExecute = 0x20000000,
GenericWrite = 0x40000000,
GenericRead = 0x80000000
}
[Flags]
public enum EFileShare : uint
{
None = 0x00000000,
Read = 0x00000001,
Write = 0x00000002,
Delete = 0x00000004
}
public enum ECreationDisposition : uint
{
New = 1,
CreateAlways = 2,
OpenExisting = 3,
OpenAlways = 4,
TruncateExisting = 5
}
[Flags]
public enum EFileAttributes : uint
{
Readonly = 0x00000001,
Hidden = 0x00000002,
System = 0x00000004,
Directory = 0x00000010,
Archive = 0x00000020,
Device = 0x00000040,
Normal = 0x00000080,
Temporary = 0x00000100,
SparseFile = 0x00000200,
ReparsePoint = 0x00000400,
Compressed = 0x00000800,
Offline = 0x00001000,
NotContentIndexed = 0x00002000,
Encrypted = 0x00004000,
Write_Through = 0x80000000,
Overlapped = 0x40000000,
NoBuffering = 0x20000000,
RandomAccess = 0x10000000,
SequentialScan = 0x08000000,
DeleteOnClose = 0x04000000,
BackupSemantics = 0x02000000,
PosixSemantics = 0x01000000,
OpenReparsePoint = 0x00200000,
OpenNoRecall = 0x00100000,
FirstPipeInstance = 0x00080000
}
[
StructLayout(LayoutKind.Sequential)
]
public struct LUID
{
public UInt32 LowPart;
public Int32 HighPart;
}
[
StructLayout(LayoutKind.Sequential)
]
public struct LUID_AND_ATTRIBUTES
{
public LUID Luid;
public UInt32 Attributes;
}
[
StructLayout(LayoutKind.Sequential)
]
public struct TOKEN_PRIVILEGES
{
public UInt32 PrivilegeCount;
[
MarshalAs(UnmanagedType.ByValArray, SizeConst = ANYSIZE_ARRAY)
]
public LUID_AND_ATTRIBUTES[] Privileges;
}
[
DllImport("Advapi32.dll", SetLastError = true)
]
public static extern bool OpenProcessToken(IntPtr ProcessHandle, UInt32 DesiredAccess, out IntPtr TokenHandle);
[
DllImport("Advapi32.dll", SetLastError = true, CharSet = CharSet.Auto)
]
[
return: MarshalAs(UnmanagedType.Bool)
]
public static extern bool LookupPrivilegeValue(string lpSystemName, string lpName, out LUID lpLuid);
[
DllImport("Advapi32.dll", SetLastError = true)
]
[
return: MarshalAs(UnmanagedType.Bool)
]
public static extern bool AdjustTokenPrivileges(IntPtr TokenHandle, [MarshalAs(UnmanagedType.Bool)] bool DisableAllPrivileges, [MarshalAs(UnmanagedType.Struct)] ref TOKEN_PRIVILEGES NewState, UInt32 BufferLength, IntPtr PreviousState, /*IntPtr*/ UInt32 ReturnLength);
[
DllImport("Kernel32.dll")
]
public static extern IntPtr GetCurrentProcess();
[
DllImport("Kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)
]
public static extern IntPtr CreateFile(string lpFileName, EFileAccess dwDesiredAccess, EFileShare dwShareMode, IntPtr lpSecurityAttributes, ECreationDisposition dwCreationDisposition, EFileAttributes dwFlagsAndAttributes, IntPtr hTemplateFile);
[
DllImport("Kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)
]
static extern IntPtr CreateFile(string fileName, [MarshalAs(UnmanagedType.U4)] FileAccess fileAccess, [MarshalAs(UnmanagedType.U4)] FileShare fileShare, IntPtr securityAttributes, [MarshalAs(UnmanagedType.U4)] FileMode creationDisposition, int flags, IntPtr template);
[
DllImport("Kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)
]
static extern bool SetFileShortName(IntPtr hFile, string lpShortName);
[
DllImport("Kernel32.dll", SetLastError = true)
]
[
return: MarshalAs(UnmanagedType.Bool)
]
static extern bool CloseHandle(IntPtr hObject);
/// <summary>
/// 程序入口.
/// </summary>
static void Main(string[] args)
{
if (Adjust(SE_RESTORE_NAME, true))
{
Console.Write(Modify(@"c:\测试文件示例.txt", "TEST~1.TXT"/*d:\TEST~1.TXT*/) ? "成功" : "失败");
Console.Write(Modify(@"c:\测试文~1.TXT", "TEST~1.TXT") ? "成功" : "失败");
Adjust(SE_RESTORE_NAME, false);
}
Console.Read();
}
/// <summary>
/// 调整特权.
/// </summary>
static bool Adjust(string privilege, bool enabled)
{
IntPtr hToken; bool result = false;
if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, out hToken))
{
LUID privilegeId;
if (LookupPrivilegeValue(null, privilege, out privilegeId))
{
TOKEN_PRIVILEGES prives = new TOKEN_PRIVILEGES();
prives.PrivilegeCount = 1;
prives.Privileges = new LUID_AND_ATTRIBUTES[ANYSIZE_ARRAY];
prives.Privileges[0].Luid = privilegeId;
prives.Privileges[0].Attributes = enabled ? SE_PRIVILEGE_ENABLED : 0;
result = AdjustTokenPrivileges(hToken, false, ref prives, (UInt32)Marshal.SizeOf(typeof(TOKEN_PRIVILEGES)), IntPtr.Zero, 0);
}
CloseHandle(hToken);
}
return result;
}
/// <summary>
/// 修改名称.
/// </summary>
static bool Modify(string path, string filename)
{
IntPtr handle = CreateFile(path, EFileAccess.GenericAll, EFileShare.None, IntPtr.Zero, ECreationDisposition.OpenExisting, EFileAttributes.BackupSemantics, IntPtr.Zero);
if (handle != new IntPtr(-1))
{
bool result = SetFileShortName(handle, filename);
CloseHandle(handle);
return result;
}
return false;
}
}
}
const uint GENERIC_ALL = (uint)0x10000000L;
const uint FILE_FLAG_BACKUP_SEMANTICS = 0x02000000;
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
namespace ModifyShortFilename
{
class Program
{
[
DllImport("Kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)
]
static extern IntPtr CreateFile(string fileName, [MarshalAs(UnmanagedType.U4)] FileAccess fileAccess, [MarshalAs(UnmanagedType.U4)] FileShare fileShare, IntPtr securityAttributes, [MarshalAs(UnmanagedType.U4)] FileMode creationDisposition, int flags, IntPtr template);
[
DllImport("Kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)
]
static extern bool SetFileShortName(IntPtr hFile, string lpShortName);
[
DllImport("Kernel32.dll", SetLastError = true)
]
[
return: MarshalAs(UnmanagedType.Bool)
]
static extern bool CloseHandle(IntPtr hObject);
static void Main(string[] args)
{
Console.Write(Modify(@"d:\测试文件示例.txt", "TEST~1.TXT"/*d:\TEST~1.TXT*/) ? "成功" : "失败");
//Console.Write(Modify(@"d:\测试文~1.TXT", "TEST~1.TXT") ? "成功" : "失败");
Console.Read();
}
static bool Modify(string path, string filename)
{
IntPtr handle = CreateFile(path, FileAccess.ReadWrite, FileShare.None, IntPtr.Zero, FileMode.Open, 0, IntPtr.Zero);
if (handle != new IntPtr(-1))
{
bool result = SetFileShortName(handle, filename);
CloseHandle(handle);
return result;
}
return false;
}
}
}