Example #1
0
 /// <summary>
 /// 创建文件夹
 /// </summary>
 /// <param name="DirFullPath">文件夹全路径</param>
 /// <param name="DirOperateOption">选项</param>
 /// <returns></returns>
 public static bool CreateDir(string DirFullPath, DirectoryOption Option)
 {
     try
     {
         if (Directory.Exists(DirFullPath))
         {
             if (Option == DirectoryOption.ExistDelete)
             {
                 Directory.Delete(DirFullPath, true);
             }
             else
             {
                 return(false);
             }
         }
         Directory.CreateDirectory(DirFullPath);
         return(true);
     }
     catch
     {
         return(false);
     }
 }
Example #2
0
        /**
         * @ 创建目录类型的缓存,主要用于监控文件数量
         * @ key 缓存的键
         * @ path 路径,请确保路径存在
         * @ offset 缓存有效时间
         * @ searchPattern 搜索匹配关键字,默认:*
         * @ searchOption 文件搜索选项,默认TopDirectoryOnly
         * @ direcotryOption 目录缓存选项,默认FileAmountOnly
         * @ priority 逐出缓存优先级策略
         * */
        public static string CreateDirectoryCache(string key, string path, DateTimeOffset offset, string searchPattern = "*", SearchOption searchOption = SearchOption.TopDirectoryOnly, DirectoryOption direcotryOption = DirectoryOption.FileAmountOnly, CacheItemPriority priority = CacheItemPriority.Default)
        {
            if (string.IsNullOrEmpty(key))
            {
                throw new ArgumentNullException("key不能为空");
            }
            if (string.IsNullOrEmpty(path))
            {
                throw new ArgumentNullException("path不能为空");
            }
            bool isNotExists = Directory.Exists(path);

            if (isNotExists)
            {
                throw new ArgumentNullException(string.Format("指定目录:{0}不存在!", path));
            }

            object values = GLCacheExpiration.Cache.Get(key);

            if (values == null)
            {
                CacheItemPolicy policy = new CacheItemPolicy();
                List <string>   fp     = new List <string>()
                {
                    path
                };

                HostFileChangeMonitor monitor = new HostFileChangeMonitor(fp);
                policy.AbsoluteExpiration = offset;
                policy.Priority           = priority;
                policy.ChangeMonitors.Add(monitor);
                string[] files = Directory.GetFiles(path, searchPattern, searchOption);

                if (direcotryOption == DirectoryOption.FileAmountOnly)
                {
                    values = files == null ? 0 : files.Length;
                }
                else if (direcotryOption == DirectoryOption.FileNameArray)
                {
                    values = files;
                }
                CacheItem chitem = new CacheItem(key, values);
                Cache.AddOrGetExisting(chitem, policy);
            }

            return(values.ToString());
        }