Dear All,
Please tell me
1.how to get the avialable domains.
2.how to get active users for each domain.
Regards
Network Programming
1 Answer
Know the answer? Post it. Somebody with the same question will find it here.
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Sam HobbsRank #107 · 20.5K reputationPosted Nov 10, 2009, 12:07 PM EST
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;
}
}
}