获取网络中所有工作组名称【c#】
这个是我的程序源码,但是运行起来跑不出个所以然来。看了会资料,没找出原因,特来请教~!!!using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.DirectoryServices;
namespace GetNetworks
{
public partial class FGetNetworks : Form
{
public FGetNetworks()
{
InitializeComponent();
}
private void btGet_Click(object sender, EventArgs e)
{
DirectoryEntry NetGroup = new DirectoryEntry("WinNT:");
foreach (DirectoryEntry domain in NetGroup.Children)
{
listBox1.Text = "";
listBox1.Items.Add(domain.Name);
}
}
}
} --------------------编程问答-------------------- 网上找的一个:
// 用于 Windows XP and W2K3--------------------编程问答-------------------- 我想知道的是
using System;
using System.Management;
class App {
public static void Main() {
SelectQuery query = new SelectQuery("Win32_ComputerSystem");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
foreach (ManagementObject mo in searcher.Get()) {
if((bool)mo["partofdomain"] != true)
Console.WriteLine("Workgroup {0} ",mo["workgroup"]);
else
Console.WriteLine("Domain {0} ",mo["workgroup"]);
}
}
}
// 用于 Windows NT and Windows 98 (WMI core redistributable required)
using System;
using System.Management;
class App {
public static void Main() {
SelectQuery query = new SelectQuery("Win32_ComputerSystem");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
foreach (ManagementObject mo in searcher.Get()) {
Console.WriteLine("{0} ",mo["domain"]); // this returns the domain name or workgroup name
}
}
}
我的这段代码,哪里有错那?
private void btGet_Click(object sender, EventArgs e)
{
DirectoryEntry NetGroup = new DirectoryEntry("WinNT:");
foreach (DirectoryEntry domain in NetGroup.Children)
{
listBox1.Text = "";
listBox1.Items.Add(domain.Name);
}
}
--------------------编程问答-------------------- up --------------------编程问答-------------------- up --------------------编程问答-------------------- 我这运行了十几秒显示一个WorkGroup出来 --------------------编程问答-------------------- 学习进行中..... --------------------编程问答-------------------- up --------------------编程问答-------------------- 我用那个方法也是只显示了个Workgroup啊,根本没有找到工作组,应该怎么做啊??
补充:.NET技术 , C#