private string GetCachePath(int size = 0, CachePathType?pathType = null)
        {
            string cachePath = "";

            CachePathType cachePathLocation = pathType.HasValue ? pathType.Value : (CachePathType)settings.GetSettingInt(Shared.Enums.UserSettingEnum.PreferredCachePath);

            switch (cachePathLocation)
            {
            case CachePathType.ApplicationFolder:
                cachePath = Path.Combine(AppContext.BaseDirectory, CacheFolderName);
                break;

            case CachePathType.UserDataFolder:
                cachePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "STLShowcase", CacheFolderName);
                break;
                //case CachePathType.UserImagesFolder:
                //    cachePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyPictures), "STLShowcase", CacheFolderName);
                //    break;
            }

            if (size > 0)
            {
                cachePath = Path.Combine(cachePath, size.ToString());
            }

            return(cachePath);
        }
示例#2
0
        private string GetCachePath(int size = 0)
        {
            string cachePath = "";

            CachePathType cachePathLocation = (CachePathType)settings.GetSettingInt(Shared.Enums.UserSettingEnum.PreferredCachePath);

            switch (cachePathLocation)
            {
            case CachePathType.ApplicationFolder:
                cachePath = Path.Combine(AppContext.BaseDirectory, CacheFolderName);
                break;

            case CachePathType.UserDataFolder:
                cachePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), CacheFolderName);
                break;

            case CachePathType.UserImagesFolder:
                cachePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyPictures), CacheFolderName);
                break;
            }

            if (size > 0)
            {
                cachePath = Path.Combine(cachePath, size.ToString());
            }
            logger.Info("Using cache folder {cachePath}", cachePath);

            return(cachePath);
        }
        public bool MoveCacheToNewLocation(CachePathType oldPath, CachePathType newPath)
        {
            string currentCachePath = GetCachePath(pathType: oldPath);
            string newCachePath     = GetCachePath(pathType: newPath);

            try
            {
                logger.Info("Moving cache folder from [{currentCachePath}] to [{newCachePath}]", currentCachePath, newCachePath);

                IEnumerable <string> cacheFiles = UtilMethods.EnumerateFiles(currentCachePath, "*.png", SearchOption.AllDirectories).ToArray();

                if (Path.GetPathRoot(currentCachePath) == Path.GetPathRoot(newCachePath))
                {
                    Directory.Move(currentCachePath, newCachePath);
                }
                else
                {
                    if (!Directory.Exists(newCachePath))
                    {
                        Directory.CreateDirectory(newCachePath);
                    }

                    foreach (string cacheFile in cacheFiles)
                    {
                        var    imageSize   = Path.GetDirectoryName(cacheFile).Split(Path.DirectorySeparatorChar).Last();
                        string newImageDir = Path.Combine(newCachePath, imageSize);

                        if (!Directory.Exists(newImageDir))
                        {
                            Directory.CreateDirectory(newImageDir);
                        }

                        File.Copy(cacheFile, Path.Combine(newImageDir, Path.GetFileName(cacheFile)), true);
                    }

                    foreach (string cacheFile in cacheFiles)
                    {
                        File.Delete(cacheFile);
                    }

                    Directory.Delete(currentCachePath, true);
                }
            }
            catch (Exception ex)
            {
                logger.Debug(ex, "Error when moving cache folder from [{currentCachePath}] to [{newCachePath}]", currentCachePath, newCachePath);
                return(false);
            }
            return(true);
        }