Open remote shared folder with credentials

前端 未结 2 1848
生来不讨喜
生来不讨喜 2021-01-20 06:16

I need to open a folder on a remote server with different credentials in a window (explorer.exe).

I managed to do it with no credentials (my credentials), but when I

2条回答
  •  感动是毒
    2021-01-20 06:55

    The answer given is a very long-winded solution that is unnecessary. I know the answer is from 2011, but all you need to do is the following:

    Public Sub Open_Remote_Connection(ByVal strComputer As String, ByVal strUsername As String, ByVal strPassword As String)
        Try
            Dim procInfo As New ProcessStartInfo
            procInfo.FileName = "net"
            procInfo.Arguments = "use \\" & strComputer & "\c$ /USER:" & strUsername & " " & strPassword
            procInfo.WindowStyle = ProcessWindowStyle.Hidden
            procInfo.CreateNoWindow = True
    
            Dim proc As New Process
            proc.StartInfo = procInfo
            proc.Start()
            proc.WaitForExit(15000)
        Catch ex As Exception
            MsgBox("Open_Remote_Connection" & vbCrLf & vbCrLf & ex.Message, 4096, "Error")
        End Try
    End Sub
    

    and then this function to actually open the C$ share:

    Private Sub OpenCDriveofPC(ByVal compName As String)
        Try
            If isPingable(compName) Then
                Open_Remote_Connection(compName, strUserName, strPassword)
                Process.Start("explorer.exe", "\\" & compName & "\c$")
            End If
        Catch ex As Exception
            MsgBox("OpenCDriveofPC" & vbCrLf & vbCrLf & ex.message, 4096, "Error")
        Finally
            Close_Remote_Connection("net use \\" & compName & "\c$ /delete /yes")
        End Try
    

    And here is the 'Close_Remote_Connection' sub, which needs to be called so that you don't make your net use list get crazy huge. Even if you call this sub, you will still have full admin rights to the c$ you open:

    Public Sub Close_Remote_Connection(ByVal device As String)
        Shell("cmd.exe /c " & device, vbHidden)
    End Sub
    

    I looked all over the Internet for how to do this and no one came even close to this simplicity. It does exactly what you want and is crazy simple and not long-winded with all sorts of crazy functions/classes that just are not needed to do this simple thing.

    Hope it helps others like it helped me! :)

    LilD

提交回复
热议问题