我试图识别文件是PNG或JPG何时将其应用为壁纸。 我使用SHGetFileInfo来获取带有.szTypeName变量的类型名称,但我刚刚意识到如果操作系统是另一种语言,它会发生变化。
这是我的代码:
SHFILEINFOW fileInfo; UINT sizeFile = sizeof(fileInfo); UINT Flags = SHGFI_TYPENAME | SHGFI_USEFILEATTRIBUTES; // Getting file info to find out if it has JPG or PNG format SHGetFileInfoW(argv[1], 0, &fileInfo, sizeFile, Flags);这就是我的验证方式:
if (wcscmp(fileInfo.szTypeName, L"JPG File") == 0) { //Code here }当操作系统是西班牙语时,值将更改为“Archivo JPG”,因此我必须对所有语言进行验证,并且没有意义。
知道我可以使用的其他功能吗?
I am trying to identify when a file is PNG or JPG to apply it as a wallpaper. I am using the SHGetFileInfo to get the type name with the .szTypeName variable, but I just realized that it changes if the OS is in another language.
This is my code:
SHFILEINFOW fileInfo; UINT sizeFile = sizeof(fileInfo); UINT Flags = SHGFI_TYPENAME | SHGFI_USEFILEATTRIBUTES; // Getting file info to find out if it has JPG or PNG format SHGetFileInfoW(argv[1], 0, &fileInfo, sizeFile, Flags);This is how I am validating:
if (wcscmp(fileInfo.szTypeName, L"JPG File") == 0) { //Code here }When the OS is in spanish, the value changes to "Archivo JPG" so I would have to validate against all language, and does not make sense.
Any idea what other function I can use?
最满意答案
此API旨在用于为已知文件类型1)生成面向用户的字符串表示形式。 它并不意味着用于实现代码逻辑。
更重要的是,它不会尝试解析文件内容 。 它仅使用文件扩展名。 如果您将Excel工作簿MySpreadsheet.xlsx重命名为MySpreadsheet.png ,它将很高兴地报告,这是一个“PNG文件” 。
问题的解决方案很简单:除了过滤文件扩展名外,您无需执行任何操作。 使用PathFindExtension (或Windows 8及更高版本的PathCchFindExtension )从完全限定的路径名获取文件扩展名。
如果用户附加了错误的文件扩展名,则可能会失败。 可以说,这不是你的应用程序应该修复的东西。
SHGFI_USEFILEATTRIBUTES ,您将 SHGFI_USEFILEATTRIBUTES传递给 SHGetFileInfoW但决定不将任何 文件属性 (第二个参数)传递给调用。 这是一个错误。 请参阅 SHGFI_USEFILEATTRIBUTES的含义是什么? 详情。
1) 它是SHGFI_DISPLAYNAME的道德等价SHGFI_DISPLAYNAME 。 您可以使用显示名称唯一显示它们 。
This API is meant to be used to produce a user-facing string representation for known file types1). It is not meant to be used to implement code logic.
More importantly, it doesn't try to parse the file contents. It works off of the file extension alone. If you rename an Excel workbook MySpreadsheet.xlsx to MySpreadsheet.png, it will happily report, that this is a "PNG File".
The solution to your problem is simple: You don't have to do anything, other than filtering on the file extension. Use PathFindExtension (or PathCchFindExtension for Windows 8 and above) to get the file extension from a fully qualified path name.
This can fail, in case the user appended the wrong file extension. Arguably, this isn't something your application should fix, though.
As an aside, you pass SHGFI_USEFILEATTRIBUTES to SHGetFileInfoW but decided to not pass any file attributes (second argument) to the call. This is a bug. See What does SHGFI_USEFILEATTRIBUTES mean? for details.
1) It is the moral equivalent of SHGFI_DISPLAYNAME. The only thing you can do with display names is display them.
更多推荐
发布评论