asp.netdirectory-structurenet-usedrive-mapping

Why 'The local device name is already in use' in asp.net when mapping drive but OK with command line


In command line it works fine: net USE T: \123.45.67.89\TEST mytestuser /user:MYTESTUSER

But using asp.net, with the following code, the Exception with the following error is always thrown: 'The local device name is already in use' when mapping drive

I have tried disconnecting from all drives first, mapping to a different Drive letter. I have done this from different servers, and to different servers

Is the problem definitely with drive letter?

temp = MapDrive("T", "\\123.45.67.89\TEST", "MYTESTUSER", "mytestuser")


Public Shared Function MapDrive(ByVal DriveLetter As String, ByVal Path As String, ByVal Username As String, ByVal Password As String) As Boolean


    Dim ReturnValue As Boolean = False

    Dim p As New System.Diagnostics.Process()
    p.StartInfo.UseShellExecute = False
    p.StartInfo.CreateNoWindow = True
    p.StartInfo.RedirectStandardError = True
    p.StartInfo.RedirectStandardOutput = True
    p.StartInfo.FileName = "net.exe"
    p.StartInfo.Arguments = " use " & DriveLetter & ": " & Path & " /user:" & Username & " " & Password & " /persistent:yes"
    p.Start()
    p.WaitForExit()
    Dim ErrorMessage As String = p.StandardError.ReadToEnd()
    Dim OuputMessage As String = p.StandardOutput.ReadToEnd()
    Dim StoreFile As System.IO.Directory
    Dim Files As String()
    Dim File As String


If ErrorMessage.Length > 0 Then
        Throw New Exception("Error:" & ErrorMessage)

    Else
        ReturnValue = True
    End If

     Files = StoreFile.GetFiles("T:\FINDTHIS\", "*")


     For Each File In Files

        HttpContext.Current.Trace.Warn(File)
     Next


    Return ReturnValue
End Function

Solution

  • Aren't mapped drives particular to the user that is running the net command?

    Your asp.net page usually runs under a different identity to the current user's. This means that your code might possibly map the network path successfully the first time you run it. Then when you run it the second time you will get that error.

    But then you are looking in vain to delete the mapping for that letter in your cmd window since the drive was not mapped via your user identity (under which your cmd window is running after all)

    Then there's also the permissions needed to run the net command, although that might have thrown a different error.