Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version
Go to top

Network Shares and UNC paths

, 11 Nov 2003
Classes to enumerate network shares on local and remote machines, and convert local file paths to UNC paths.
networkshares_src.zip
testShares.exe
using System;
using System.IO;
using Trinet.Networking;

/// <summary>
/// A console app to test the Share class.
/// </summary>
class SharesTest
{
	/// <summary>
	/// The main entry point for the application.
	/// </summary>
	[STAThread]
	static void Main(string[] args)
	{
		TestShares();
	}
	
	static void TestShares() 
	{
		// 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 
					{
						DirectoryInfo d = si.Root;
						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.");
		
		Console.WriteLine();
		
		// Enumerate shares on a remote computer
		Console.Write("Enter the NetBios name of a server on your network: ");
		string server = Console.ReadLine();
		
		if (server != null && server.Trim().Length > 0)
		{
			Console.WriteLine("\nShares on {0}:", server);
			shi = ShareCollection.GetShares(server);
			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 you don't have permissions to the share,
					// you will get security 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.ToString(), ex.Message);
						}
					}
				}
			}
			else
				Console.WriteLine("Unable to enumerate the shares on {0}.\n"
					+ "Make sure the machine exists, and that you have permission to access it.",
					server);
			
			Console.WriteLine();
		}
		
		// Resolve local paths to UNC paths.
		string fileName = string.Empty;
		do
		{
			Console.Write("Enter the path to a file, or \"Q\" to exit: ");
			fileName = Console.ReadLine();
			if (fileName != null && fileName.Length > 0) 
			{
				if (fileName.ToUpper() == "Q") fileName = string.Empty;
				else
				{
					Console.WriteLine("{0} = {1}", fileName, ShareCollection.PathToUnc(fileName));
				}
			}
			
		} while (fileName != null && fileName.Length > 0);
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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

Share

About the Author

Richard Deeming
Software Developer Nevalee Business Solutions
United Kingdom United Kingdom
No Biography provided
Follow on   Twitter   Google+

| Advertise | Privacy | Mobile
Web01 | 2.8.140921.1 | Last Updated 12 Nov 2003
Article Copyright 2002 by Richard Deeming
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid