Solved

Enumerate Shares on a Server - VB.NET

Posted on 2010-08-20
5
1,285 Views
Last Modified: 2012-05-10
I would like to be able to enumerate the [disk] shares for a given server name.  I have seen some tips and sample code "out there" but it seems to be all old and decrepit.

Regards,

John



0
Comment
Question by:JXCovert
  • 3
  • 2
5 Comments
 
LVL 8

Expert Comment

by:SylvainDrapeau
ID: 33489707
Hello !

I've done it once using VB.NET and WMI. I can post the code if you're interested.

Syldra
0
 

Author Comment

by:JXCovert
ID: 33490126
I would be VERY intrigued to see your code, Sylvain.
0
 
LVL 8

Accepted Solution

by:
SylvainDrapeau earned 500 total points
ID: 33490312
Hello !

I extracted the code from a large source so it's not the cleanest you've seen but it's clear enough I guess.

The code was made to query the local computer only but you can replace "\\.\root\cimv2" on the first line by "\\SERVER\root\cimv2" to query a remote server.

Call the function QueryShare to get an arraylist containing every share in the form "Share name (share path)". If you want only disk shares (and not path shares), add a filter around the "'Adds to result" command.

Syldra
Dim mScope As New ManagementScope("\\.\root\cimv2")
    Dim Searcher As ManagementObjectSearcher
    Dim Items As ManagementObjectCollection
   
    'Connect to WMI, exits if error
        Try
            mScope.Connect()
        Catch ex As Exception
            OutputWMIError(ex.Message)
            Exit Sub
        End Try
   
   Function QueryShare() As ArrayList
        Dim Result As New ArrayList
 
        'Lists all local shares
        Searcher = New ManagementObjectSearcher("SELECT Name, Path, Type FROM Win32_Share Where Type='0'")
        Searcher.Scope = mScope

        Try
            Items = Searcher.Get
            For Each moComputer As ManagementObject In Items
                'Adds to results
                Result.Add(moComputer("Name") & " (" & moComputer("Path") & ")")
            Next
        Catch ex As Exception
            Result.Add("Error : " & ex.Message)
        End Try
        Return Result
    End Function

Open in new window

0
 

Author Closing Comment

by:JXCovert
ID: 33614795
This worked perfectly for Window Server shares, but not for NAS shares.
0
 

Author Comment

by:JXCovert
ID: 33614796
Thanks, Sylvain,

John

0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question