I need to map a network drive via code and found that .Net cannot do this directly so I tied to use API calls. Below is a start. The problem is that none of the errors are defined. To find the one that is shown, I forced an error then stepped through the code. There has to be a better way.
VB Code:
  1. Private Sub MapDrive(ByVal strDrive As String, ByVal strPath As String)
  2.  
  3.         Dim err As Long
  4.         Dim ERROR_ALREADY_ASSIGNED As Long = 8192874795449188437
  5.  
  6.  
  7.         err = WNetAddConnection(strPath, "", strDrive)
  8.  
  9.         Select Case (err)
  10.             Case ERROR_ALREADY_ASSIGNED
  11.                 WNetCancelConnection(strDrive, CType(True, Integer))
  12.                 WNetAddConnection(strPath, "", strDrive)
  13.  
  14.         End Select
  15.  
  16.     End Sub

I figured that the mapped drive was simply a key in the registry and I was correct (sort of). I can do this and create a key in the registry.
VB Code:
  1. Private Sub MapDrive(ByVal strDrive As String, ByVal strPath As String)
  2.  
  3.         Dim regKey As RegistryKey = _
  4.             Registry.CurrentUser.CreateSubKey("Network\" & strDrive)
  5.         If Not (regKey Is Nothing) Then
  6.             regKey.SetValue("ConnectionType", 1)
  7.             regKey.SetValue("DeferFlags", 4)
  8.             regKey.SetValue("ProviderName", "Microsoft Windows Network")
  9.             regKey.SetValue("ProviderType", 131072)
  10.             regKey.SetValue("RemotePath", strPath)
  11.             regKey.SetValue("UserName", 0)
  12.         End If
  13.  
  14.     End Sub

This makes a correct entry into the registry and the newly mapped drive appears in both the "Map Network Drive" and "Disconnect Network Drive" dialogs but it does not appear in "My Computer". I like this method because it modifies the registry even if the drive already exists. Can someone shed some light on what I'm missing with the registry approach?