static private void WriteCacheFile() { ActiveDirCacheFile cache = new ActiveDirCacheFile(); cache.DomainName = Environment.UserDomainName; cache.Computers = Computers; FileStream file = new FileStream(Directory.GetCurrentDirectory() + "\\adcache.xml", FileMode.Create, FileAccess.Write); XmlSerializer serializer = new XmlSerializer(typeof(ActiveDirCacheFile)); serializer.Serialize(file, cache); file.Close(); }
static public void GetComputers() { LocalNetworkSettings.GetLocalIPAddress(); if (Environment.UserDomainName.ToUpper() == Environment.MachineName) { adNotAvailable = true; return; } try { // get root of the directory data tree on a directory server DirectoryEntry dirEntry = new DirectoryEntry("LDAP://rootDSE"); // get the hostname of the directory server of your root (I'm assuming that's what you want) string dnsHostname = dirEntry.Properties["dnsHostname"].Value.ToString(); IPAddress[] ipAddresses = Dns.GetHostAddresses(dnsHostname); Ping pingSender = new Ping(); for (int i = 0; i < ipAddresses.Count(); i++) { try { PingReply reply = pingSender.Send(ipAddresses[i].ToString(), 1000); if (reply.Status == IPStatus.Success) { DCAvailable = true; break; } } catch {} } } catch { } cacheFound = false; CachedAD.Clear(); if (File.Exists("adcache.xml")) { ActiveDirCacheFile cache = LoadCacheFile(); if (Environment.UserDomainName == cache.DomainName) { cacheFound = true; LastUpdate = File.GetLastWriteTime("adcache.xml"); foreach (String s in cache.Computers) { CachedAD.Add(s); } } } if (cacheFound && Settings.AUTO_UPDATE_AD_ON_STARTUP) { Thread UpdateThread = new Thread(new ThreadStart(BeginADDownload)); UpdateThread.Start(); } else if (cacheFound && !Settings.AUTO_UPDATE_AD_ON_STARTUP) { AppConsole.WriteLine("Cached AD was loaded."); } else { if (adUpToDate) { MessageBox.Show("Please wait!\n\nError: Cahced AD was not found.\n\nClick OK to continue. This process can take several minutes.", "Active Directory Updating", MessageBoxButtons.OK, MessageBoxIcon.Warning); } BeginADDownload(); } }