WSH获取驱动器名称

时间:2014-01-24 17:44:14

标签: javascript windows-xp wsh

以下代码段适用于Windows 8但不适用于Windows XP:

function listDrives()
{
    /* Drive type:
    0   Unknown
    1   No Root Directory
    2   Removable Disk
    3   Local Disk
    4   Network Drive
    5   Compact Disc
    6   RAM Disk
    */

    try {
        var wmi = GetObject("winmgmts:{impersonationLevel=impersonate}");
        for(var e = new Enumerator(wmi.InstancesOf("Win32_LogicalDisk")); !e.atEnd(); e.moveNext()) {
            var s = e.item();   
            WScript.Echo(s.Name + " ; " + s.DriveType + " ; " + s.VolumeName);
        }
    }
    catch (err) {
        WScript.Echo(err.message);
    }
}

在Win8下,列出了所有驱动器。在XP下,没有输出或错误。有谁知道为什么?感谢。

编辑:我正在测试的WinXP用户是Adminsitrators组的一部分。

1 个答案:

答案 0 :(得分:1)

仅供参考,这是一个权限问题 - 我无法解决。所以,我从here得出了这个WSH解决方案:

var objFSO = new ActiveXObject("Scripting.FileSystemObject");
for(var e = new Enumerator(objFSO.Drives); !e.atEnd(); e.moveNext()) {
    var objDrive = e.item();
    WScript.Echo("Available space: " + objDrive.AvailableSpace);
    WScript.Echo("Drive letter: " + objDrive.DriveLetter);
    WScript.Echo("Drive type: " + objDrive.DriveType);
    WScript.Echo("File system: " + objDrive.FileSystem);
    WScript.Echo("Free space: " + objDrive.FreeSpace);
    WScript.Echo("Is ready: " + objDrive.IsReady);
    WScript.Echo("Path: " + objDrive.Path);
    WScript.Echo("Root folder: " + objDrive.RootFolder);
    WScript.Echo("Serial number: " + objDrive.SerialNumber);
    WScript.Echo("Share name: " + objDrive.ShareName);
    WScript.Echo("Total size: " + objDrive.TotalSize);
    WScript.Echo("Volume name: " + objDrive.VolumeName);
}