在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) } }
在electron中引入diskinfo,因为electron它允许使用Node.js。