【问题标题】:Get desktop file icons using NodeJs使用 NodeJs 获取桌面文件图标
【发布时间】:2020-08-30 10:37:20
【问题描述】:

我正在尝试在 Electron 中创建一个桌面启动器应用程序,该应用程序读取用户桌面中的文件数量并从找到的每个文件中收集信息。我正在收集文件并根据目录构建路径,但我能得到的只是文件名。我不确定如何存储文件本身并从中提取桌面图标。除了使用 AxtiveXobject 之外,我还没有看到很多解决方案,但是据说某些实现在最新的 nodejs 中不起作用。到目前为止,这是我的代码。

//requiring path and fs modules
const path = require('path');
const fs = require('fs');
//gets home directory
const homedir = require('os').homedir();
//specifies to desktop
const dir = `${homedir}/Desktop`;
var walk = require('walk');
var filepaths = [];
//storing desktop path
var desktopDir = dir;
console.log(desktopDir);
//gets the desktop files and paths
function getDesktopFiles(_dir){
    //read directory
    fs.readdir(_dir, (err, files) => { 
        if (err) 
          console.log(err); 
        else { 
            files.forEach(_file => { 
            //console.log(_file); 
            let _p = _dir + '/'+_file;
            //changes slashing for file paths
            let _path = _p.replace(/\\/g, "/");
            filepaths.push(_path);

          }) 
        } 
    }) 
    for(let p of filepaths){
        console.log(p);
    }

}

getDesktopFiles(desktopDir);

【问题讨论】:

    标签: javascript node.js electron fs


    【解决方案1】:

    这是一个快速的 sn-p 代码,适用于 Electron 渲染器进程;它已经在 macOS 和 Linux 上成功测试过,应该是平台无关的。

    它列出了位于用户桌面上的所有文件,并在 HTML 页面的末尾显示每个文件的图标和名称;它使用以下 Electron API 函数:

    const { app, nativeImage } = require ('electron').remote;
    const path = require ('path');
    const fs = require ('fs');
    //
    const desktopPath = app.getPath ('desktop');
    let filePaths = fs.readdirSync (desktopPath);
    for (let filePath of filePaths)
    {
        app.getFileIcon (filePath)
        .then
        (
            (fileIcon) =>
            {
                let div = document.createElement ('div');
                let img = document.createElement ('img');
                img.setAttribute ('src', fileIcon.toDataURL ());
                let size = fileIcon.getSize ();
                img.setAttribute ('width', size.width);
                img.setAttribute ('height', size.height);
                div.appendChild (img);
                div.appendChild (document.createTextNode (" " + path.basename (filePath)));
                // For test purposes, add each file icon and name to the end of <body>
                document.body.appendChild (div);
            }
        );
    }
    

    您可能会在帖子中找到一些关于app.getFileIcon 的有趣提示:Is there a standard way for an Electron or Node.js app to access system-level icons?

    【讨论】:

    • 我不知道 electron 内置了文件图标功能。我会确保检查他们的文档。这实际上是一个比我设法汇集的更好的实现。谢谢!
    【解决方案2】:

    为相同的定义一个函数:

    function load(icon) {
      if (cache[icon]) return cache[icon];
      return cache[icon] = fs.readFileSync(__dirname + '/public/icons/' + icon, 'base64');
    }
    

    Here你可以得到同样的灵感。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-08-08
      • 2020-02-04
      • 1970-01-01
      • 2023-02-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多