Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.6k views
in Technique[技术] by (71.8m points)

electron 如果获取windows盘符 以及空间

想要获取windows 的盘符和总大小剩余大小, electron 中该如何获取,


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

在electron中引入diskinfo,因为electron它允许使用Node.js。

const diskinfo = require('diskinfo');
// 当前盘符
const current_disk = __dirname.substr(0,2).toLowerCase();
// 获得所有磁盘空间
diskinfo.getDrives(function (err, aDrives) {
    for (let i = 0; i < aDrives.length; i++) {
        // 只获取当前磁盘信息
    if (aDrives[i].mounted.toLowerCase() == current_disk)   {
        // 盘符号
        const mounted = 'mounted ' + aDrives[i].mounted;
        // 总量
        const total = 'total ' + (aDrives[i].blocks / 1024 / 1024 /   1024).toFixed(0) + 'gb';
        // 已使用
        const used = 'used ' + (aDrives[i].used / 1024 / 1024 / 1024).toFixed(0) + 'gb';
        // 可用
        const available = 'available ' + (aDrives[i].available / 1024 / 1024 / 1024).toFixed(0) +'gb';
        // 使用率
        const capacity = 'capacity ' + aDrives[i].capacity;
        console.log('盘符号:',mounted)
        console.log('总量:',total)
        console.log('已使用:',used)
        console.log('可用:',available)
        console.log('使用率:',available)
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...