Kin*_*gon 18 c# c++ networking winapi
我正在尝试获取本地网络计算机的列表.我尝试使用NetServerEnum
和WNetOpenEnum
API,但两个API都返回错误代码6118 (ERROR_NO_BROWSER_SERVERS_FOUND)
.不使用本地网络中的Active Directory.
奇怪的Windows资源管理器显示所有本地计算机没有任何问题
还有其他方法可以获取局域网中的计算机列表吗?
小智 14
您将需要使用System.DirectoryServices命名空间并尝试以下操作:
DirectoryEntry root = new DirectoryEntry("WinNT:");
foreach (DirectoryEntry computers in root.Children)
{
foreach (DirectoryEntry computer in computers.Children)
{
if (computer.Name != "Schema")
{
textBox1.Text += computer.Name + "\r\n";
}
}
}
Run Code Online (Sandbox Code Playgroud)
它对我有用.
Kin*_*gon 11
我使用接口IShellItem和CSIDL_NETWORK找到了解决方案.我得到了所有的网络电脑.
C++:使用方法IShellFolder :: EnumObjects
C#:您可以使用Gong Solutions Shell Library
using System.Collections;
using System.Collections.Generic;
using GongSolutions.Shell;
using GongSolutions.Shell.Interop;
public sealed class ShellNetworkComputers : IEnumerable<string>
{
public IEnumerator<string> GetEnumerator()
{
ShellItem folder = new ShellItem((Environment.SpecialFolder)CSIDL.NETWORK);
IEnumerator<ShellItem> e = folder.GetEnumerator(SHCONTF.FOLDERS);
while (e.MoveNext())
{
Debug.Print(e.Current.ParsingName);
yield return e.Current.ParsingName;
}
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
Run Code Online (Sandbox Code Playgroud)
我用它做了一个功能.本SchemaClassName
必须是计算机
public List<string> NetworkComputers()
{
return (
from Computers
in (new DirectoryEntry("WinNT:")).Children
from Computer
in Computers.Children
where Computer.SchemaClassName == "Computer"
orderby Computer.Name
select Computer.Name).ToList;
}
Run Code Online (Sandbox Code Playgroud)