局域网得到所有计算机名称的问题,我看了以前的贴子,自己没有成功。一定给分

Sean_JIN 2002-10-16 04:34:52
能不能给出具体的代码或有代码的连接。
System.Net.IPAddress[] addressList =
Dns.GetHostByName(Dns.GetHostName()).AddressList;
中的
addressList[i].ToString()只是本机的地址。
...全文
93 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
kamphkb 2003-03-10
  • 打赏
  • 举报
回复
// 这是测试代码,请在正式使用中配合异常处理机制。

using System;
using System.Runtime.InteropServices;

namespace EnumNetPcs
{
[StructLayoutAttribute(LayoutKind.Sequential, CharSet=CharSet.Auto)]
public class NetResource
{
public UInt32 Scope;
public UInt32 RType;
public UInt32 Display;
public UInt32 Usage;

[MarshalAs(UnmanagedType.LPTStr)]
public string LocalName;
[MarshalAs(UnmanagedType.LPTStr)]
public string RemoteName;
[MarshalAs(UnmanagedType.LPTStr)]
public string Comment;
[MarshalAs(UnmanagedType.LPTStr)]
public string Provider;
}

class Test
{
[DllImport("mpr.dll", CharSet=CharSet.Auto)]
private static extern Int32 WNetOpenEnum
(
UInt32 scope,
UInt32 type,
UInt32 usage,
[In] NetResource rsc,
out IntPtr handle
);

[DllImport("mpr.dll", CharSet=CharSet.Auto)]
private static extern Int32 WNetEnumResource
(
IntPtr handle,
ref Int32 count,
IntPtr buffer,
ref Int32 size
);

[DllImport("mpr.dll", CharSet=CharSet.Auto)]
private static extern Int32 WNetCloseEnum(IntPtr handle);


[STAThread]
static void Main()
{
Int32 result = -1;
IntPtr handle = IntPtr.Zero;

result = WNetOpenEnum(0x00000005, 0x00000000, 0x00000004, null, out handle);
if(result != 0) return;

Int32 entries = 0;
Int32 memory = 16300;
IntPtr buffer = Marshal.AllocHGlobal(memory);
Type type = typeof(NetResource);
Int32 size = Marshal.SizeOf(type);
NetResource rsc = null;
Int32 currect = 0;
while(true)
{
entries = -1;
memory = 16300;
result = WNetEnumResource(handle, ref entries, buffer, ref memory);
if(result >= 259)
{
// Finished.
break;
}
else if(result != 0 || entries < 1)
{
// Failed.
break;
}

currect = (Int32) buffer;
for(Int32 i = 0; i < entries; i++)
{
rsc = (NetResource) Marshal.PtrToStructure((IntPtr) currect, type);
if(rsc != null)
if(rsc.RemoteName != null)
if(rsc.RemoteName.Length > 2)
if(rsc.RemoteName.Substring(0,2).Equals(@"\\"))
Console.WriteLine("RemoteName: {0}", rsc.RemoteName.Substring(2));
currect += size;
}
}

Marshal.FreeHGlobal(buffer);
result = WNetCloseEnum(handle);
Console.Read();
}
}
}
TheAres 2003-01-16
  • 打赏
  • 举报
回复
用ADSI实现,

可以将路径指定为 "WinNT: "枚举出一个局域网内所有的域,如下列代码所示:

DirectoryEntry objSpace = new DirectoryEntry( "WinNT: ");
foreach(DirectoryEntry domain in objSpace.Children)
System.Diagnostics.Debug.WriteLine(domain.Name);

如果要枚举一个组中的所有成员,首先要激活其Members成员,并查询(映射到)他的IEnumerable接口。然后遍历其中每一个元素,生成相应的入口对象并输出。如下列代码所示:

string domainName = "Domain ";
string groupName = "Group ";
DirectoryEntry group = new DirectoryEntry( "WinNT:// " + domainName + "/ " + groupName + ",group ");

foreach(Object member in (IEnumerable)group.Invoke( "Members "))
{
DirectoryEntry dirmem = new DirectoryEntry(member);
Console.WriteLine(dirmem.Name);
}
aspcn 2003-01-16
  • 打赏
  • 举报
回复
ADSI,方法与TheArea说的一样。

绝对能成功,不要怀疑,就看你写的程序对不对,不管你是不是域管理员,只要你在这个域内。
chinchy 2003-01-16
  • 打赏
  • 举报
回复
http://www.c-sharpcorner.com/Code/2002/Aug/ListAllComps.asp
ahmy_0 2003-01-16
  • 打赏
  • 举报
回复
gz
chenbinghui 2003-01-16
  • 打赏
  • 举报
回复
gz
aaxu 2003-01-16
  • 打赏
  • 举报
回复
你有网域管理员权限吗?
Jasonchen82 2003-01-16
  • 打赏
  • 举报
回复
有个方法:
可以通过循环IP来得到计算机名
System.Net.Dns.GetHostByAddress(“IP地址”).HostName
kamphkb 2003-01-15
  • 打赏
  • 举报
回复
up
yarshray 2002-10-16
  • 打赏
  • 举报
回复
wmi
Sean_JIN 2002-10-16
  • 打赏
  • 举报
回复
都是得到本机的IP,这个早知道了。
我要得到当前局域网的所有计算机的名称和IP,用遍隶地址的方法也是不可取的,因为时间太长。
yarshray 2002-10-16
  • 打赏
  • 举报
回复
http://www.ccidnet.com/tech/guide/2001/11/15/58_3742.html
http://www.chinabs.net/csharp/default.asp?infoid=27

111,116

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧