Solved

Enumerate Shares on a Server - VB.NET

Posted on 2010-08-20
5
1,263 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

919 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now