Recently someone told me, he couldn't get the name of the computers on the network! so I tried to create and used some .net libraries but i couldn't get the name of the computer either.
I'm only getting the I.P. address of computers on the certain network, so I look for some articles that could help me out! and found this micorosft's documentation click here
As you can see I only have two computers here! it works out for me! I hope this sample helps someone out !!!!
Sample Code
clsComputerNetworks.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
namespace getComputerNetworkNames
{
///
/// for micorosoft's documentation
/// http://msdn.microsoft.com/en-us/library/aa370623(VS.85).aspx
/// another helpful site
/// http://www.pinvoke.net/default.aspx/netapi32.netserverenum
/// http://win32.mvps.org/index.html
/// when i searched in google i found this good article at codeproject
/// http://www.codeproject.com/KB/IP/ListNetworkComputers.aspx
///
public class clsComputerNetworks
{
#region constants
public const uint ERROR_SUCCESS = 0;
public const uint ERROR_MORE_DATA = 234;
public enum SV_101_TYPES:uint
{
SV_TYPE_WORKSTATION= 0x00000001,
SV_TYPE_SERVER= 0x00000002,
SV_TYPE_SQLSERVER = 0x00000004,
SV_TYPE_DOMAIN_CTRL= 0x00000008,
SV_TYPE_DOMAIN_BAKCTRL= 0x00000010,
SV_TYPE_TIME_SOURCE= 0x00000020,
SV_TYPE_AFP= 0x00000040,
SV_TYPE_NOVELL= 0x00000080,
SV_TYPE_DOMAIN_MEMBER = 0x00000100,
SV_TYPE_PRINTQ_SERVER = 0x00000200,
SV_TYPE_DIALIN_SERVER = 0x00000400,
SV_TYPE_XENIX_SERVER = 0x00000800,
SV_TYPE_SERVER_UNIX = 0x00000800,
SV_TYPE_NT = 0x00001000,
SV_TYPE_WFW = 0x00002000,
SV_TYPE_SERVER_MFPN= 0x00004000,
SV_TYPE_SERVER_NT = 0x00008000,
SV_TYPE_POTENTIAL_BROWSER = 0x00010000,
SV_TYPE_BACKUP_BROWSER= 0x00020000,
SV_TYPE_MASTER_BROWSER= 0x00040000,
SV_TYPE_DOMAIN_MASTER = 0x00080000,
SV_TYPE_SERVER_OSF= 0x00100000,
SV_TYPE_SERVER_VMS= 0x00200000,
SV_TYPE_WINDOWS= 0x00400000,
SV_TYPE_DFS= 0x00800000,
SV_TYPE_CLUSTER_NT= 0x01000000,
SV_TYPE_TERMINALSERVER= 0x02000000,
SV_TYPE_CLUSTER_VS_NT = 0x04000000,
SV_TYPE_DCE= 0x10000000,
SV_TYPE_ALTERNATE_XPORT= 0x20000000,
SV_TYPE_LOCAL_LIST_ONLY= 0x40000000,
SV_TYPE_DOMAIN_ENUM= 0x80000000,
SV_TYPE_ALL= 0xFFFFFFFF
};
#endregion
[StructLayout(LayoutKind.Sequential)]
public struct _SERVER_INFO_100
{
internal int sv100_platform_id;
[MarshalAs(UnmanagedType.LPWStr)]
internal string sv100_name;
}
[DllImport("netapi32.dll", CharSet = CharSet.Auto, EntryPoint = "NetServerEnum", SetLastError = true)]
public static extern int NetServerEnum([MarshalAs(UnmanagedType.LPWStr)]string servername,
int level,
out IntPtr bufptr,
int prefmaxlen,
ref int entriesread,
ref int totalentries,
SV_101_TYPES servertype,
[MarshalAs(UnmanagedType.LPWStr)]string domain,
IntPtr resume_handle);
[DllImport("netapi32.dll", EntryPoint = "NetApiBufferFree")]
public static extern int NetApiBufferFree(IntPtr buffer);
private static IntPtr bufferRecData = IntPtr.Zero;
private static IntPtr Buffer_ = IntPtr.Zero;
private static int entriesEnumerated = 0;
private static int totalComputersInNetwork = 0;
private static IntPtr resHandle = IntPtr.Zero;
private static int sizeofINFO = Marshal.SizeOf(typeof(_SERVER_INFO_100));
public static List<string> getNetworkComputers()
{
List<string> networkComputers = new List<string>();
try
{
int ret = NetServerEnum(null, 100, out bufferRecData,
-1,
ref entriesEnumerated,
ref totalComputersInNetwork,
SV_101_TYPES.SV_TYPE_WORKSTATION | SV_101_TYPES.SV_TYPE_SERVER,
null,
resHandle);
if (ret == 0)
{
for (int intCounter = 0; intCounter < totalComputersInNetwork; intCounter++)
{
Buffer_ = new IntPtr((int)bufferRecData +
(intCounter * sizeofINFO));
_SERVER_INFO_100 svrInfo = (_SERVER_INFO_100)
Marshal.PtrToStructure(Buffer_,
typeof(_SERVER_INFO_100));
networkComputers.Add(svrInfo.sv100_name);
}
}
}
catch (Exception ex)
{
throw;
}
finally
{
NetApiBufferFree(bufferRecData);
}
return networkComputers;
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace getComputerNetworkNames
{
public partial class frmComputerName : Form
{
public frmComputerName()
{
InitializeComponent();
}
private void btnGetComputerName_Click(object sender, EventArgs e)
{
try
{
if (this.lstComputerName.Items.Count > 0)
this.lstComputerName.Items.Clear();
List ComputerName = clsComputerNetworks.getNetworkComputers();
foreach (string name in ComputerName)
{
this.lstComputerName .Items.Add(name);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
2 comments:
hey...ur code is good...can you help me with getting the username means the user who has logged in to that system....is that possible...reply ASAP..thanks..in advance
hmmmm! i'll try to make one then i'm going to post it here!
Post a Comment