devxlogo

Use UNC Names to Refer to Network Drives

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, \SERVERVOLUME ) 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 LongPublic 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		=> LocalMicrosoft	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 iEnd Function

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

 Option ExplicitPublic Sub OpenDatabase()	'The Wrong Way!	Dim filespec as String	filespec = "H:PUBLICAPPSBTRIEVEMYDB.RDB"	'Some third-party control that opens the 	'database file.	...	...	... End SubPublic 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" 	Else		filespec = Vol1Drive & _			"PUBLICAPPSBTRIEVEMYDB.RDB"		'Some third-party control that opens 		'the database file. 		...		... 		...	End IfEnd Sub
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist