int entries;
int buffer = 16384;
IntPtr ptrBuffer = Marshal.AllocHGlobal(buffer);
NETRESOURCE nr;
for(;;)
{
entries = -1;
buffer = 16384;
iRet =WNetEnumResource(ptrHandle,ref entries,ptrBuffer,ref buffer);
if((iRet != 0) || (entries<1))break;
Int32 ptr = ptrBuffer.ToInt32();
for(int i=0;i<entries;i++ )
{
nr = (NETRESOURCE)Marshal.PtrToStructure( new IntPtr(ptr), typeof(NETRESOURCE) );
if(RESOURCE_USAGE.RESOURCEUSAGE_CONTAINER == (nr.dwUsage & RESOURCE_USAGE.RESOURCEUSAGE_CONTAINER))
{
ptr += Marshal.SizeOf( nr );
if(nr.dwDisplayType == RESOURCE_DISPLAYTYPE.RESOURCEDISPLAYTYPE_NETWORK)
{
pNode1 = tView.Nodes.Add(nr.lpRemoteName);//TreeView
pNode1.ImageIndex = 0;
pNode1.SelectedImageIndex = 0;
}
else if(nr.dwDisplayType == RESOURCE_DISPLAYTYPE.RESOURCEDISPLAYTYPE_DOMAIN)
{
pNode2 = pNode1.Nodes.Add(nr.lpRemoteName);
pNode2.ImageIndex = 1;
pNode2.SelectedImageIndex =1;
}
else if(nr.dwDisplayType == RESOURCE_DISPLAYTYPE.RESOURCEDISPLAYTYPE_SERVER)
{
string sPCName = nr.lpRemoteName;
pNode3 = pNode2.Nodes.Add(sPCName.Substring(2,sPCName.Length-2));\\此处为去处机器名称前的两个"\\"字符
pNode3.ImageIndex = 2;
pNode3.SelectedImageIndex = 2;
}
WNETOE(nr);
}
}
}
Marshal.FreeHGlobal( ptrBuffer );
iRet =WNetCloseEnum( ptrHandle );
}
catch(Exception ex)
{
MessageBox.Show("查找本地网络错误:"+ex.Message,"错误",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
}
#endregion
//窗体LOAD事件中,使用函数
private void frmDBConnConfig_Load(object sender, System.EventArgs e)
{
WNETOE(null);
}
作者主页:itbaby.jss.cn