本文介绍了如何使用golang获取Windows上所有驱动器的清单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我的程序遍历Windows系统上的所有驱动器并搜索特定的文件类型.现在,我可以运行该程序并向其传递驱动器号,但是我希望它自动在所有驱动器上搜索.目前,我需要执行以下操作:

I would like my program to iterate through all drives on a Windows system and search for a particular file type. Right now, I can run the program and pass it a drive letter to start from, but I want it to search on all drives automatically. Currently, I would need to do something like this:

C:\> program.exe C:
C:\> program.exe D:
C:\> program.exe E:

我希望程序获取所有驱动器的列表并遍历所有驱动器,而无需用户指定驱动器号.使用Go可以做到吗?

I want the program to get a list of all drives and iterate through all of them without the user having to specify the drive letter. Is this possible using Go?

类似于此问题列出所有物理驱动器(Windows),但改用Go C.

Similar to this question Listing All Physical Drives (Windows) but using Go instead of C.

推荐答案

简便的方法是编写自己的函数,并尝试打开Volker提到的驱动器"文件夹.

The easist way is write own function with try to open "drive" folder mentioned by Volker.

import "os"

func getdrives() (r []string){
    for _, drive := range "ABCDEFGHIJKLMNOPQRSTUVWXYZ"{
        f, err := os.Open(string(drive)+":\\")
        if err == nil {
            r = append(r, string(drive))
            f.Close()
        }
    }
    return
}

这篇关于如何使用golang获取Windows上所有驱动器的清单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 19:49