我正在使用SHGetFileInfo
检索有关文件或目录的某些信息,即图标或文件扩展名的描述。
检索文件扩展名描述时,SHGetFileInfo
返回的字符串缺少前四个字符。
例如,.pdf
文件的描述是 Adobe Acrobat Document
,但我只获得 e Acrobat Document
或 的描述.exe
文件是 Anwendung
(因为我是德国人,我想在英语中它是 Application
),但我只得到 ndung
.
我正在使用
public static string GetFileTypeDescription(this FileInfo file)
{
SHFILEINFO shFileInfo;
if (SHGetFileInfo(
file.Extension,
SHGFI_FILE_ATTRIBUTE_NORMAL,
out shFileInfo,
(uint)Marshal.SizeOf(typeof(SHFILEINFO)),
SHGFI_USEFILEATTRIBUTES | SHGFI_TYPENAME)
!= IntPtr.Zero)
{
return shFileInfo.szTypeName;
}
return null;
}
使用SHGetFileInfo
的通常实现:
[DllImport("shell32.dll")]
internal static extern IntPtr SHGetFileInfo(string pszPath, uint dwFileAttributes, out SHFILEINFO psfi, uint cbSizeFileInfo, uint uFlags);
[StructLayout(LayoutKind.Sequential)]
internal struct SHFILEINFO
{
public IntPtr hIcon;
public IntPtr iIcon;
public uint dwAttributes;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
public string szDisplayName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)]
public string szTypeName;
};
//I omitted all flags which are not used above
private const uint SHGFI_FILE_ATTRIBUTE_NORMAL = 0x80,
SHGFI_USEFILEATTRIBUTES = 0x10,
SHGFI_TYPENAME = 0x400;
出了什么问题?我错过了什么?或者我如何检索完整的描述?
最佳答案
C++ 结构中的 iIcon 字段的类型为 int。在 Windows 上,这是一个 4 字节有符号整数。相当于C#中的int。
您已在 C# 代码中将该字段声明为 IntPtr。这是一个有符号整数,与指针大小相同。所以在 32 位代码中是 4 个字节,在 64 位代码中是 8 个字节。您似乎正在运行 64 位代码。
因此,错误是该字段的声明类型错误。解决办法是将iIcon的类型改为int。
关于c# - SHGetFileInfo : Description for a files extension too short,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20152273/