Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: Visual Basic
Expertise: Intermediate
Sep 17, 1998



Building the Right Environment to Support AI, Machine Learning and Deep Learning

Use UNC Names to Refer to Network Drives

If you write programs that access information on a network server, you never want to hard-code drive letters. Not all software vendors take advantage of the Uniform Naming Convention (UNC) (for example, \\SERVER\VOLUME\ ) technology. Use these API declarations:

 Public Declare Function GetVolumeInformation _
	Lib "kernel32" Alias "GetVolumeInformationA" ( _
	ByVal lpRootPathName As String, _
	ByVal lpVolumeNameBuffer As String, _
	ByVal nVolumeNameSize As Long, _
	lpVolumeSerialNumber As Long, _
	lpMaximumComponentLength As Long, _
	lpFileSystemFlags As Long, _
	ByVal lpFileSystemNameBuffer As String, _
	ByVal nFileSystemNameSize As Long) As Long

Public Function GetNetworkDrive(FileSystem _
	As String, VolumeName As String) As String

	'Returns the first mapped drive letter 
	'if successful or "NODRIVE" if fails.

	'FileSystem and SysName refer to the 
	'Network file system type: 
	'NWCOMPA	=> Novell
	'NTFS	=> Microsoft
	'FAT		=> Local\Microsoft

	Dim SerialNum As Long, SysFlags As Long
	Dim retVal As Long, Complength As Long
	Dim VolBuff As String * 255, SysName As String * 255
	Dim DrivePath As String
	Dim i As Integer

	GetNetworkDrive = "NODRIVE"

	For i = 70 To 90
		DrivePath = Chr(i) & ":\"
		retVal = GetVolumeInformation _
			(DrivePath, VolBuff, 255, _
			SerialNum, Complength, SysFlags, SysName, 255)
		If (Mid$(VolBuff, 1, Len(VolumeName)) _
			= UCase$(VolumeName)) And(Mid$(SysName,1, Len _
			(FileSystem)) = FileSystem) Then
				GetNetworkDrive = DrivePath
				Exit For
		End If
	Next i
End Function

Here's the wrong and right ways to refer to network drives:

 Option Explicit

Public Sub OpenDatabase()
	'The Wrong Way!
	Dim filespec as String
	'Some third-party control that opens the 
	'database file.
End Sub

Public Sub OpenDatabase()
	'A Better Way!
	Dim filespec as String
	Dim Vol1Drive As String
	Vol1Drive = GetNetworkDrive("NWCOMPA","VOL1")
	If Vol1Drive = "NODRIVE" Then 
		MsgBox "Unable to open database. " & _
			"The user does not have a " & _
			"drive letter mapped to VOL1" 
		filespec = Vol1Drive & _
		'Some third-party control that opens 
		'the database file. 
	End If
End Sub
Charles Douglas
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date