查找分配给主驱动器的字母

我试图find像Environ这样的function来find我的业务中的主要驱动器已经映射到特定的PC上的驱动器。

使用文件path“G:\ Eworking \ SET \ Operations \ file”我知道我的电脑已经映射,以便该文件path在G驱动器内,但其他可能映射不同,所以我想确定它是哪一个。

我已经尝试了如果其他方法通过字母表,然后做一个if Dir([filepath]) then之前(见下文),但我想知道是否有一个更好的方式做到这一点?

 Sub LoopThroughDrives() sFilePath As String sFilePath = ":\Eworking\SET\Operations\file" If Dir("A" & sFilePath) > 0 Then msgbox ("It's in drive A") Else If Dir("B" & sFilePath) > 0 Then msgbox ("It's in drive B") Else If Dir("C" & sFilePath) > 0 Then msgbox ("It's in drive C") Else '........................... 'All other letters of the alphabet, checking each possibility '........................... End If End If End If End Sub 

对于…下一个使用ASCII字符的循环似乎是适当的。

 dim c as integer for c = 65 to 90 If CBool(Len(Dir(Chr(c) & sFilePath))) Then msgbox "It's in drive " & Chr(c) '<~~msgbox, not magbox exit for end if next c if c > 90 then msgbox "never found" 

你可以得到这样的驱动器号和path:

 For Each d In CreateObject("Scripting.FileSystemObject").Drives Debug.Print d.DriveLetter, d.Path, d.ShareName Next