Click here to Skip to main content
Click here to Skip to main content

Network Shares and UNC paths

By , 11 Nov 2003
 

Introduction

Two common requirements seem to have been missed from the .NET framework: enumerating network shares, and converting a local path to a UNC path. The shares could potentially be retrieved via WMI, but you can't guarantee that NT4 and 98 will have it installed. These classes provide a simple wrapper around the API calls necessary to retrieve this information.

Implementation

To retrieve the UNC path for a file on a mapped drive, call WNetGetUniversalName. This doesn't work on Windows 95, but neither does the .NET framework, so I don't care!

If the path is on a local shared folder, WNetGetUniversalName won't help. In this case, you need to call NetShareEnum and look for a matching path. The function behaves completely differently across NT/2K/XP and 9x/ME, so there are two versions of this code.

  • In the NT version, you may not have permissions to access the SHARE_INFO_2 information, so the code will revert to the SHARE_INFO_1 structure. In this case, the path will not be available.
  • On Windows 9x/ME/NT4, the server name (if any) must be prefixed with "\\". The code will check this, so you don't need to worry.
  • On Windows 9x/ME, there is no way to resume the enumeration, so the code will return a maximum of 20 shares.

Classes

Share

Encapsulates information about a single network share, including the server name, share name, share type, local path and comment. Also has some utility methods to determine if it is a file-system share, whether it matches part of a local path, and returns the root directory.

ShareCollection

A strongly-typed read-only collection of Share objects. Shares can be retrieved by index or by path, in which case the best match will be returned. Includes factory methods to return the shares for a specified computer, and static methods to return the UNC path and local share for a local path.

Interesting Note

The Windows 98 structure SHARE_INFO_50 is declared in the file SrvApi.h. Towards the top of the file, there is a line which reads #pragma pack(1). Although this looks like complete gobbledegook, it is actually important. The number in brackets specifies the packing for all structures in the file. According to MSDN, the alignment of a member will be on a boundary that is either a multiple of n or a multiple of the size of the member, whichever is smaller.

In the case of the SHARE_INFO_50 structure, the default packing, pads the structure to align the members on 8 byte boundaries. This meant that the size was calculated as 44 bytes, when it should be 42. Two extra padding bytes are added to the end of the structure to make the length a multiple of 4, the size of the native integer.

After tearing my hair out trying to work out why the size was wrong, and why taking two bytes off the end reduced the size by four bytes, I finally noticed the Pack property of the StructLayoutAttribute class. Adding Pack=1 to the attribute fixed the problems, and the code now works on Windows 98.

Demonstration

The testShares.cs file contains a sample console application which demonstrates the main functionality. The testShares.exe file is the compiled version of this sample.

//
// Enumerate shares on local computer
//
Console.WriteLine("\nShares on local computer:");
ShareCollection shi = ShareCollection.LocalShares;
if (shi != null) 
{
    foreach(Share si in shi) 
    {
        Console.WriteLine("{0}: {1} [{2}]", 
            si.ShareType, si, si.Path);

        // If this is a file-system share, try to
        // list the first five subfolders.
        // NB: If the share is on a removable device,
        // you could get "Not ready" or "Access denied"
        // exceptions.
        if (si.IsFileSystem) 
        {
            try 
            {
                System.IO.DirectoryInfo d = si.Root;
                System.IO.DirectoryInfo[] Flds = d.GetDirectories();
                for (int i=0; i < Flds.Length && i < 5; i++)
                    Console.WriteLine("\t{0} - {1}", i, Flds[i].FullName);

                Console.WriteLine();
            }
            catch (Exception ex) 
            {
                Console.WriteLine("\tError listing {0}:\n\t{1}\n", 
                    si, ex.Message);
            }
        }
    }
}
else
    Console.WriteLine("Unable to enumerate the local shares.");

//
// Resolve local paths to UNC paths.
//
Console.WriteLine("{0} = {1}", 
    fileName, ShareCollection.PathToUnc(fileName));

Updates

  • 25 September 2002 - Original Release
  • 13 March 2003 -
    • Changed the ShareCollection to inherit from System.Collections.ReadOnlyCollection.
    • Moved the P/Invoke code inside the ShareCollection class, and removed the internal Interop class.
    • Fixed the packing bug on Windows 9x. Note to self: When the C++ header file says #pragma pack(1), don't ignore it!
    • Added support for level 1 share enumeration on Windows 98, which is required to list shares on remote machines, but isn't documented in MSDN.
  • 12 November 2003 - Bug fix

Credits

This code is (very) loosely based on a VB6 solution by Karl E. Peterson [http://www.mvps.org/vb].

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Richard Deeming
Software Developer Nevalee Business Solutions
United Kingdom United Kingdom
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 5mvpJohn Simmons / outlaw programmer17 Nov '12 - 10:44 
Brilliant code and exactly what I needed. My only change was to make the ShareCollection a List<Share> so I could remove shares that were found that I wasn't interested in (default shares, non-disk shares, and stuff like that.
 
I'm using your code in my media center manager software to control where the user can search for media. Since the path is stored in a database that can be accessed from any PC on the LAN, I need to force the user to select from available UNC paths. Your code helps find the shares after I've found all the machines on the LAN.
 
Great stuff. Smile | :)
GeneralMy vote of 5memberRomulus-Tours28 Feb '11 - 5:14 
Very nice and helpfull
GeneralFail of ShareCollection.GetShares("10.0.255.13"); and ShareCollection.GetShares(@"\\10.0.255.13"); in VistamemberJosep Maria Roy7 Jan '10 - 3:59 
Hi,
 
good article! congratulations.   Wink | ;)
 
Just I'm using it from my web app with Framework 3.5 and Vista and I'm getting this errors:
 
ShareCollection shi = ShareCollection.GetShares("computer_name");   = works fine: 8 shared resources!
ShareCollection shi = ShareCollection.GetShares("10.0.255.13");      = fail
ShareCollection shi = ShareCollection.GetShares(@"\\10.0.255.13"); = fail
 
computer_name has 10.0.255.13 ip   Shucks | :->
 
The issue is in this lines:
 
                        nRet = NetShareEnum(server, level, out pBuffer, -1,
                              out entriesRead, out totalEntries, ref hResume);
 
                        if (ERROR_ACCESS_DENIED == nRet)
                        {
                              //Need admin for level 2, drop to level 1
                              level = 1;
                              nRet = NetShareEnum(server, level, out pBuffer, -1,
                                    out entriesRead, out totalEntries, ref hResume);
                        }
 
in 2nd nRet = NetShareEnum, nRet is still returning ERROR_ACCESS_DENIED. I have tested in some computers of my network and my conclusion is when you are using IP NetShareEnum seems that is not working fine in Vista (not tested neither XP nor Windows 7).
 
Talking of Windows 7: is this class compatible? Confused | :confused:
 
Yours sincerely,
 
Josep Balague
GeneralRe: Fail of ShareCollection.GetShares("10.0.255.13"); and ShareCollection.GetShares(@"\\10.0.255.13"); in VistamemberRichard Deeming7 Jan '10 - 4:32 
Strange - it works for me with either the computer name or IP address, from a console app or a web app. (I'm running Windows 7, so I guess that answers your last question.)
 
The documentation[^] says it requires "the DNS or NetBIOS name of the remote server", but the IP address (without the "\\" prefix) also seems to work.
 
Have you tried using WMI / System.Management[^]? A "Select * From Win32_Share[^]" query should return the list of shares.
 



"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer


Questionsam thing in java??membergaurav_nanda21 Mar '08 - 4:50 
hey !! can u suggest me how can i do same job in java???
its very important,,, please any one
GeneralLicence issues ... UNC classesmembervin kerai18 Dec '07 - 15:42 
Hi,
 
Can anyone please advise of any licence issues with regards to using these classes.
 
Regards
ViN.
GeneralRe: Licence issues ... UNC classesmemberThunderWeasel26 Jul '11 - 7:54 
This might be about 3 years too late, but you can find the licensing information here[^]. (There is a link at the top of the current page as well.)
Mark

GeneralFunctionality is too robust for needsmembervachaun19 Apr '07 - 8:45 
This works fine, but it retrieves too much data for what I need. Is there a way to get only the items that would be listed if you browsed to the location using the UNC path of the server...
 
IE: Start -> Run -> \\server_name -> enter
 
and list only items that are shown here.
GeneralRe: Functionality is too robust for needsmemberRichard Deeming19 Apr '07 - 8:52 
You can filter out any Share instances where the IsFileSystem property is false, which will leave you with the file-system shares.
 
If you want to hide the administrative shares as well, you can filter out any items where the ShareType property is equal to ShareType.Special.
 

"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

GeneralRe: Functionality is too robust for needsmembervachaun19 Apr '07 - 9:17 
That's exactly what I needed, with just a bit more fine tuning it gave me exactly what I wanted. Great Code!!!

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130523.1 | Last Updated 12 Nov 2003
Article Copyright 2002 by Richard Deeming
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid