Welcome to OStack Knowledge Sharing 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 Answer

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)
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...