?
Solved

Enumerate Shares on a Server - VB.NET

Posted on 2010-08-20
5
Medium Priority
?
1,407 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

Application Discovery Service in AWS

In the era of the cloud, customers migrating away from their existing on-premise infrastructure. This requires lots of planning, strategies, and effort to identify their existing resources and determine how best to migrate.  Datacenter migrations happen in four phases -

Question has a verified solution.

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

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses

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