コード例 #1
0
        private static OrganizationResult GetCachedOrganization(Int32 organizationID, Server server, bool ignoreDate)
        {
            OrganizationResult result = null;

            try
            {
                string path = XmlCachePath + Path.DirectorySeparatorChar + OrganizationPath;
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
                string file = path + Path.DirectorySeparatorChar + (int)server + "." + organizationID + ".xml";
                if (File.Exists(file))
                {
                    TimeSpan time = DateTime.Now - File.GetLastWriteTime(file);
                    if (time.TotalHours > CacheDuration && ignoreDate == false)
                    {
                        return(null);
                    }

                    FileStream   stream = null;
                    StreamReader reader = null;
                    try
                    {
                        stream = File.OpenRead(file);
                        reader = new StreamReader((Stream)stream);
                        string            xml   = reader.ReadToEnd();
                        OrganizationCache cache = new OrganizationCache();
                        cache  = ParseOrganizationCache(xml);
                        result = cache.ToOrganizationResult(server);
                    }
                    catch { }
                    finally
                    {
                        if (reader != null)
                        {
                            reader.Close();
                            reader.Dispose();
                        }
                        if (stream != null)
                        {
                            stream.Close();
                            stream.Dispose();
                        }
                    }
                }
            }
            catch { }
            return(result);
        }
コード例 #2
0
        private static string OrganizationCacheToXml(OrganizationCache whois)
        {
            string       xml    = null;
            MemoryStream stream = null;

            try
            {
                XmlSerializer serializer = new XmlSerializer(typeof(OrganizationCache));
                stream = new MemoryStream();
                serializer.Serialize(stream, whois);
                xml = Encoding.UTF8.GetString(stream.ToArray());
            }
            catch { }
            finally
            {
                if (stream != null)
                {
                    stream.Close();
                    stream.Dispose();
                }
            }
            return(xml);
        }
コード例 #3
0
        private static OrganizationCache ParseOrganizationCache(string xml)
        {
            MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(xml));

            try
            {
                XmlSerializer     serializer = new XmlSerializer(typeof(OrganizationCache));
                OrganizationCache result     = (OrganizationCache)serializer.Deserialize(stream);
                stream.Close();
                stream.Dispose();
                return(result);
            }
            catch { }
            finally
            {
                if (stream != null)
                {
                    stream.Close();
                    stream.Dispose();
                }
            }
            return(null);
        }
コード例 #4
0
        private static void SetCachedOrganization(OrganizationResult organization, Server server)
        {
            if (organization.ID < 1)
            {
                return;
            }
            if (organization.Name == null || organization.Name == string.Empty)
            {
                return;
            }
            if (organization.Members == null || organization.Members.Items == null || organization.Members.Items.Length == 0)
            {
                return;
            }

            OrganizationCache cache = new OrganizationCache();

            cache.FromOrganizationResult(organization);
            string xml = OrganizationCacheToXml(cache);

            if (xml == null || xml == String.Empty)
            {
                return;
            }

            FileStream   stream = null;
            StreamWriter writer = null;

            try
            {
                string path = XmlCachePath + Path.DirectorySeparatorChar + OrganizationPath;
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
                string file = path + Path.DirectorySeparatorChar + (int)server + "." + organization.ID + ".xml";
                if (File.Exists(file))
                {
                    File.Delete(file);
                }
                stream = File.Create(file);
                writer = new StreamWriter((Stream)stream);
                writer.Write(xml);

                foreach (OrganizationMember member in organization.Members.Items)
                {
                    WhoisResult whois = member.ToWhoisResult(organization);
                    SetCachedWhois(whois, server);
                }
            }
            catch { }
            finally
            {
                if (writer != null)
                {
                    writer.Close();
                    writer.Dispose();
                }
                if (stream != null)
                {
                    stream.Close();
                    stream.Dispose();
                }
            }
        }