一、app.getFileIcon
import { app } from 'electron'
// 返回NativeImage类型
app.getFileIcon(path, { size: 'large' }).then(nativeImage=>{
// 调用toDataURL获取base64数据
const base64 = nativeImage.toDataURL()
})
像 .exe, .dll, .ico 这些本身就带图标的文件,app.getFileIcon获取的图标是符合预期的,但是像 .mp3 ,.png,.url等关联文件则返回的是系统扩展名相关联的图标。
而且快捷方式(.lnk文件)返回的也是系统默认图标,需要进一步处理 shell.readShortcutLink(path).target
获取源地址,有些还会获取失败。
二、第三方包 icon-extractor
var iconExtractor = require('icon-extractor');
iconExtractor.emitter.on('icon', function(data){
console.log('Here is my context: ' + data.Context);
console.log('Here is the path it was for: ' + data.Path);
// 获取返回的base64
console.log('Here is the base64 image: ' + data.Base64ImageData);
});
iconExtractor.getIcon('SomeContextLikeAName','c:\myexecutable.exe');
返回的是32x32的图片,优点是不用处理快捷方式。但是处理关联文件还是不行。