我正在创建一个 Electron 应用程序来帮助我管理我的磁盘空间。不过,我希望它也能在 Linux/UNIX 上运行。
我编写了以下代码,它适用于 Windows,但不适用于 Linux/UNIX 系统。
window.onload = function(){
const cp = require('child_process')
cp.exec('wmic logicaldisk get size,freespace,caption', (error, stdout)=>{
let drives = stdout.trim()split('\r\r\n')
.map(value => value.trim().split(/\s{2,0}/))
.slice(1)
})
}
输出看起来像这样。
[
["560232439808", "C:", "999526756352", "System" ]
["999369699328", "D:", "999558213632", "SSD" ]
["1511570386944", "E:", "8001545039872", "Get" ]
["4620751712256", "F:", "8001545039872", "BR" ]
["788449492992", "G:", "4000650883072", "Seen" ]
["2296009408512", "H:", "4000768323584", "Seen 2" ]
["3594248679424", "I:", "8001545039872", "2160" ]
["3507750227968", "J:", "8001545039872", "1080" ]
["945300619264", "K:", "999625322496", "Trailer" ]
]
由于我不熟悉 Linux/UNIX,我想知道如何在 Linux/UNIX 上实现相同的输出?
最佳答案
可能没有适用于所有平台的命令。
但是您可以做的是使用 process.platform
获取当前平台并在每个平台上运行不同的命令。
例如:
const cp = require('child_process');
if (process.platform == 'win32') { // Run wmic for Windows.
cp.exec('wmic logicaldisk get size,freespace,caption', (error, stdout)=>{
let drives = stdout.trim()split('\r\r\n')
.map(value => value.trim().split(/\s{2,0}/))
.slice(1)
});
} else if (process.platform == 'linux') { // Run df for Linux.
cp.exec('df', (error, stdout)=>{
// Do your magic here.
});
} else {
// Run something for a mac.
}
您可以阅读关于process.platform
here .
关于javascript - 获取所有平台上所有驱动器的已用/可用磁盘空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59548068/