Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Get "Net Use" from within VB

Status
Not open for further replies.

Ogi

Technical User
Nov 9, 2001
896
GB
Hi,

How can I get all the current network connections, drives, lpt ports etc from within VB instead of using the DOS "Net Use" command?

Cheers in advance!
Carl.
 
I don't know how to do it in VB, but I saw a link on the vbscript forum some while back and held onto the following code - I hope it helps.

QUESTION:
New to vbscript, with a question.

I currently use Net Use in a batch file to map to several network drives at
login. I would like to know if this can be converted to vbscript, and then
called at login.

In batch file, I use:

net use L: \\10.110.69.120\C$ /user:username password

I would also like this to run minimized to prevent user from seeing username and password.

Any help is appreciated.

REPLY:
In vbs, to connect network drives, do the following :
Code:
public WshNetwork
Set WshNetwork = CreateObject("WScript.Network")
WshNetwork.MapNetworkDrive "L:", "\\10.110.69.120\C$", false, username, password

REPLY:
That appears to be just what I needed, but have another question.

I need to connect to 5 remote locations (L - M local drives)at login, is this
possible? I also need to ensure that after log out, the mapping is
disconnected.

Should this be a loop? I am getting an error when trying to connect to 2
drives, says object is already in use.

Help again is appreciated.
-------------------------------------------------------------------

REPLY:
For the second question (multiple connect), there's no problem as soon as the
drive letter and mapped drive is different for each connections. Drives
connected this way doesn't survive after logoff.
Water is not bad as long as it stays out human body ;-)
-------------------------------------------------------------------

REPLY:
I was able to get the multiple drives connected, thanks.

Error I rec'ed was because the drive was taken when I was trying to run the
script again.

Do you know how I could add any error handling, to check and see if the drive is
taken?

Thanks again.
-------------------------------------------------------------------

REPLY:
Code:
public WshNetwork
Set WshNetwork = CreateObject("WScript.Network")
on error resume next 'begins error catching
WshNetwork.MapNetworkDrive "L:", "\\10.110.69.120\C$", false, username, password
if err.number <> 0 then
  'do here your error handling
  err.clear
  on error goto 0 'ends error catching
end if

REPLY: 
public WshNetwork
Set WshNetwork = CreateObject(&quot;WScript.Network&quot;)
Set fso = Wscript.CreateObject(&quot;Scripting.FileSystemObject&quot;)
if fso.DriveExists(&quot;L:&quot;) Then 
   WshNetwork.RemoveNetworkDrive &quot;L:&quot;, True
   WshNetwork.MapNetworkDrive &quot;L:&quot;, &quot;\\10.110.69.120\C$&quot;, false, username,
password
Else
   WshNetwork.MapNetworkDrive &quot;L:&quot;, &quot;\\10.110.69.120\C$&quot;, false, username,
password
End If Regards

HANDLE: saw15
POSTED ON: Oct 28, 2002

REPLY: 
How would I write a Case statement, to look for Drive letter?

REPLY: 
dletter = &quot;L:&quot;
Set WshNetwork = CreateObject(&quot;WScript.Network&quot;)
Set fso = Wscript.CreateObject(&quot;Scripting.FileSystemObject&quot;)

Select Case dletter
   Case &quot;L:&quot;
      if fso.DriveExists(dletter) Then 
         WshNetwork.RemoveNetworkDrive dletter, True
         WshNetwork.MapNetworkDrive dletter, &quot;\\10.110.69.120\C$&quot;, false,
username, password
      Else
         WshNetwork.MapNetworkDrive dletter, &quot;\\10.110.69.120\C$&quot;, false,
username, password
      End If 
   Case &quot;M:&quot;
      if fso.DriveExists(dletter) Then 
         WshNetwork.RemoveNetworkDrive dletter, True
         WshNetwork.MapNetworkDrive dletter, &quot;\\10.110.69.120\D$&quot;, false,
username, password
      Else
         WshNetwork.MapNetworkDrive dletter, &quot;\\10.110.69.120\D$&quot;, false,
username, password
      End If 
   Case Else
      if fso.DriveExists(dletter) Then 
         WshNetwork.RemoveNetworkDrive dletter, True
         WshNetwork.MapNetworkDrive dletter, &quot;\\10.110.69.120\E$&quot;, false,
username, password
      Else
         WshNetwork.MapNetworkDrive dletter, &quot;\\10.110.69.120\E$&quot;, false,
username, password
      End If 
End Select
 
TomKane,
It's easier (and more polite to the original posters) if you just include a pointer to the original thread, rather than copying and pasting, thus removing handles.
________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'People who live in windowed environments shouldn't cast pointers.'
 
Code:
Private Sub Form_Load()
Dim Network
Dim Drives
Dim Printers
    Set Network = CreateObject(&quot;WScript.Network&quot;)
    Set Drives = Network.EnumNetworkDrives
    Set Printers = Network.EnumPrinterConnections
        For i = 0 To Drives.Count - 1 Step 2
            MsgBox &quot;Connected drive: &quot; & Drives.Item(i + 1)
        Next
        For i = 0 To Printers.Count - 1 Step 2
            MsgBox &quot;Printer: &quot; & Printers.Item(i + 1) & vbCrLf & &quot;Port: &quot; & Printers.Item(i)
        Next
    MsgBox &quot;UserDomain : &quot; & Network.UserDomain & vbCrLf & _
           &quot;UserName : &quot; & Network.UserName & vbCrLf & _
           &quot;ComputerName : &quot; & Network.ComputerName, _
           vbOKOnly, &quot;Network Properties&quot;
    Unload Me
    End
End Sub
 
Hi,

Thanks for you posts!

I've used Supra's example and that does seem to get me what I require!

However, if I map a printer to LPT1/2/3/4/5 how can I find these out, it only seems to show me LPT1??

Thanks for any help in advance.
Carl.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top