Solved

Enumerate Shares on a Server - VB.NET

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…

679 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