Solved

Enumerate Shares on a Server - VB.NET

Posted on 2010-08-20
5
1,234 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
Comment Utility
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
Comment Utility
I would be VERY intrigued to see your code, Sylvain.
0
 
LVL 8

Accepted Solution

by:
SylvainDrapeau earned 500 total points
Comment Utility
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
Comment Utility
This worked perfectly for Window Server shares, but not for NAS shares.
0
 

Author Comment

by:JXCovert
Comment Utility
Thanks, Sylvain,

John

0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

743 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

12 Experts available now in Live!

Get 1:1 Help Now