Beispiel #1
0
        public string GetContent(string urlEnd, CacheType type, TimeSpan stayInCache)
        {
            string ret = null;

            try
            {
                Stuff.RemoveInvalidPathSymbols(ref urlEnd);

                string dir  = Path.Combine(cache, type.ToString()) + Path.DirectorySeparatorChar;
                string file = dir + urlEnd;

                switch (type)
                {
                case CacheType.GeocoderCache:
                    file += ".geo";
                    break;

                case CacheType.PlacemarkCache:
                    file += ".plc";
                    break;

                case CacheType.RouteCache:
                    file += ".dragdir";
                    break;

                case CacheType.UrlCache:
                    file += ".url";
                    break;

                default:
                    file += ".txt";
                    break;
                }

                if (File.Exists(file))
                {
                    var writeTime = File.GetLastWriteTime(file);
                    if (DateTime.Now - writeTime < stayInCache)
                    {
                        using (StreamReader r = new StreamReader(file, Encoding.UTF8))
                        {
                            ret = r.ReadToEnd();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                ret = null;
                Debug.WriteLine("GetContent: " + ex);
            }

            return(ret);
        }
Beispiel #2
0
        public void SaveContent(string urlEnd, CacheType type, string content)
        {
            try
            {
                Stuff.RemoveInvalidPathSymbols(ref urlEnd);

                string dir = Path.Combine(cache, type.ToString()) + Path.DirectorySeparatorChar;

                // precrete dir
                if (!Directory.Exists(dir))
                {
                    Directory.CreateDirectory(dir);
                }

                string file = dir + urlEnd;

                switch (type)
                {
                case CacheType.GeocoderCache:
                    file += ".geo";
                    break;

                case CacheType.PlacemarkCache:
                    file += ".plc";
                    break;

                case CacheType.RouteCache:
                    file += ".dragdir";
                    break;

                case CacheType.UrlCache:
                    file += ".url";
                    break;

                case CacheType.DirectionsCache:
                    file += ".dir";
                    break;

                default:
                    file += ".txt";
                    break;
                }

                using (StreamWriter writer = new StreamWriter(file, false, Encoding.UTF8))
                {
                    writer.Write(content);
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine("SaveContent: " + ex);
            }
        }