Dear All,
Please tell me
1.how to get the avialable domains.
2.how to get active users for each domain.
Regards
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Sam HobbsPosted Nov 10, 2009, 5:07 PM
Try the following console program. It is probably not the best but I think it will work. I assume you can use this as a sample.
using System.IO;
using System.DirectoryServices;
using System.Diagnostics;
namespace _70570
{
class Program
{
static StreamWriter sw;
static void Main(string[] args)
{
try { sw = new StreamWriter("70570.txt"); }
catch (Exception ex)
{
Console.WriteLine("Output file open error: {0}", ex.Message);
return;
}
DirectoryEntry de = null;
try
{
de = new DirectoryEntry("WinNT:");
foreach (DirectoryEntry Domain in de.Children)
{
sw.WriteLine("Domain: {0}", Domain.Name);
foreach (DirectoryEntry Computer in Domain.Children)
{
sw.WriteLine("\tComputer: {0}", Computer.Name);
if (GetProperty(Computer, "OperatingSystem"))
{
foreach (DirectoryEntry User in Computer.Children)
{
if (GetProperty(User, "UserFlags"))
sw.WriteLine("\t\tUser: {0}", User.Name);
User.Close();
}
}
Computer.Close();
}
Domain.Close();
}
}
catch (Exception ex)
{
Console.WriteLine("Exception: {0}", ex.Message);
sw.WriteLine("Exception: {0}", ex.Message);
}
if (de != null)
de.Close();
sw.Flush();
sw.Close();
}
private static bool GetProperty(DirectoryEntry de, string p)
{
String v = null;
try { v = de.Properties[p].Value.ToString(); }
catch (Exception ex)
{
return false;
}
sw.WriteLine("\t\t{0}: {1}", p, v);
return true;
}
}
}