Getting Shared Folders from Remote Machine

bri189a

Senior Contributor
Joined
Sep 11, 2003
Messages
1,004
Location
VA
What I have below are two classes I made to retrieve all of the machines on an address list... this in itself might be useful to somebody... but two questions... first and most obvious is there an easier way to do this... in otherwords when you click network neighborhood on your desktop, the machines that are there are there instantly... how does windows do this? Next question, and this should make sense... how to I get the shares off of the computer... again... Network Neighborhood does this instantly when you click on the remote machine and I'm kind of stuck now. What I have works, but how I'd determine the shares I don't know... I know FileDialog's work great for opening and saving... all that stuff is built it, but what I'm doing has nothing to do with opening or saving... thanks.

first the Main class where the other class is called asyncronously to speed up the wait time:
C#:
using System;
using System.Net;
using System.Threading;
namespace System
{
	public class GetIpAddress
	{
		public static void Main(string[] args) 
		{
			IpAddressing [] ip = new IpAddressing[256];
			IpDelegate [] ipDlgt = new IpDelegate[256];
			IAsyncResult [] ar = new IAsyncResult[256];
			string [] ipReturn = new string[256];
			long ipAddress = 0x000B00BE;
			for(int c=0;c<256;c++)
			{
				ip[c] = new IpAddressing();
				ipDlgt[c] = new IpDelegate(ip[c].IpAddressingExecute);
				ar[c] = ipDlgt[c].BeginInvoke(ipAddress, null, null);
				ipAddress +=  0x01000000;
			}
			//now we'll start getting what has returned...
			for(int c=0;c<256;c++)
			{
				ipReturn[c] = ipDlgt[c].EndInvoke(ar[c]);
				if(ipReturn[c]!=null)
					Console.WriteLine(ipReturn[c]);
			}
			Console.WriteLine("Complete!");
			Console.ReadLine();
		}
	}
}

Next the IpAddressing class which polls a given computer via IP address for it's hostname (the local machine name):

C#:
using System;
using System.Net;
using System.Threading; 
// The delegate must have the same signature as the method
// you want to call asynchronously.
public delegate string IpDelegate(long ipAddress);
public class IpAddressing 
	{
	// The method to be executed asynchronously.
	public string IpAddressingExecute(long ipAddress) 
	{
		//this actually reads a 0.11.0.190 which is backwards
		//long host = 0x000B00BE;
		IPAddress ip = new IPAddress(ipAddress);
		try
		{
			return ip.ToString() + " = " + Dns.GetHostByAddress(ip).HostName;
		}
		catch
		{
			return null;
		}
	}
}

Thanks for your help!
 
Back
Top