Fffish_01

导航

 

创建缓存文件,以及去重的操作

        /// <summary>
        /// 创建缓存文件
        /// </summary>
        /// <param name="path"></param>
        public static void AppendCacheFile(string path, CacheInfo cacheInfo)
        {
            string cacheFilePath = System.IO.Path.Combine(path, Const.CacheFile);
            //string entryText = JsonConvert.SerializeObject(cacheInfo, Formatting.Indented);
            _CacheScript(cacheFilePath, cacheInfo.FileEntryPath);
        }

        private static void _CacheScript(string path, string entryText)
        {
            if (File.Exists(path))
            {
                //传入json转实体列表
                JavaScriptSerializer serializer = new JavaScriptSerializer();
                List<string> entryList = serializer.Deserialize<List<string>>(File.ReadAllText(path));
                if (entryList == null)
                {
                    entryList = new List<string>();
                    entryList.Add(entryText);
                    _EntryListDeduplicateWrite(entryList, path);
                }
                else if (entryList.Count < 5)
                {
                    //如果这个json文件中的元素个数<5,则可以直接追加元素
                    entryList.Add(entryText);
                    _EntryListDeduplicateWrite(entryList, path);
                }
                else
                {
                    //删除第一个元素,然后追加
                    entryList.RemoveAt(0);
                    entryList.Add(entryText);
                    _EntryListDeduplicateWrite(entryList, path);
                }
            }
            else
            {
                System.IO.FileStream fs = System.IO.File.Create(path);
                fs.Close();
                string strResult = JsonConvert.SerializeObject(entryText, Formatting.Indented);
                File.WriteAllText(path, strResult);
            }
        }

        /// <summary>
        /// 列表元素去重并写入cache缓存文件
        /// </summary>
        /// <param name="entryList"></param>
        /// <returns></returns>
        private static void _EntryListDeduplicateWrite(List<string> entryList, string path)
        {
            Dictionary<string, bool> entryDictionary = new Dictionary<string, bool>();
            foreach (var entry in entryList)
            {
                if (entryDictionary.ContainsKey(entry))
                {
                    entryDictionary.Remove(entry);
                }
                entryDictionary.Add(entry, true);
            }

            List<string> finalList = new List<string>();
            foreach (var dicEntry in entryDictionary)
            {
                finalList.Add(dicEntry.Key);
            }

            string strResult = JsonConvert.SerializeObject(finalList, Formatting.Indented);
            File.WriteAllText(path, strResult);
        }
###Form初始化
    private void _FileEntryInit(string path)
    {
        FileEntryComba.Items.Clear();
        if (File.Exists(path))
        {
            //传入json转实体列表
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            List<string> entryList = serializer.Deserialize<List<string>>(File.ReadAllText(path));
            if (entryList != null)
            {
                foreach (var entry in entryList)
                {
                    FileEntryComba.Items.Add(entry);
                }
            }
        }
    }
posted on 2022-07-05 10:09  Fffish_01  阅读(137)  评论(0)    收藏  举报