/// <summary>
        /// 用用文件转移
        /// </summary>
        /// <param name="filePath"></param>
        /// <param name="outputPath"></param>
        public void FileTransfer(string filePath, string outputPath)
        {
            if (!File.Exists(filePath))
            {
                return;
            }
            if (string.IsNullOrWhiteSpace(outputPath) || !Directory.Exists(outputPath))
            {
                return;
            }

            string outputFilePath = Path.Combine(outputPath, CommonService.GetFileName(filePath));

            File.Move(filePath, outputFilePath);
        }
        /// <summary>
        /// 视频文件转移
        /// </summary>
        /// <param name="filePath"></param>
        /// <param name="outputPath"></param>
        public void FileTransfer(string filePath, string outputPath)
        {
            if (!File.Exists(filePath))
            {
                return;
            }
            if (string.IsNullOrWhiteSpace(outputPath) || !Directory.Exists(outputPath))
            {
                return;
            }

            //可以做视频限制,比如当视频是.mp4格式时大于XX兆才转移
            var fileExtension = CommonService.GetFileExtension(filePath);

            if (fileExtension == null)
            {
                return;
            }
            if (fileExtension.Equals("mp4"))
            {
                using (var fileStream = File.Open(filePath, FileMode.Open))
                {
                    if (fileStream.Length < _minSize)
                    {
                        return;
                    }
                }
            }

            string outputFilePath = Path.Combine(outputPath, CommonService.GetFileName(filePath));

            if (!File.Exists(outputFilePath))
            {
                File.Move(filePath, outputFilePath);
            }
            else
            {
                string fileNewName = CommonService.GetFileName(filePath).Split('.')[0] + new Random().Next(0, 9999) + CommonService.GetFileExtension(filePath);
                File.Move(filePath, Path.Combine(outputPath, fileNewName));
            }
        }
Example #3
0
        /// <summary>
        /// 图片文件转移
        /// </summary>
        /// <param name="filePath"></param>
        /// <param name="outputPath"></param>
        public void FileTransfer(string filePath, string outputPath)
        {
            //图片转移规则为:
            //若该图片路径下存在多于X张的图片,则将此路径下的所有图片文件都转移到对应的output文件夹下,并且创建一个名称为图片所在文件夹名的文件夹存放这些图片

            //说明之前的转移中已经把该图片进行转移了,因此直接返回空即可
            if (!File.Exists(filePath))
            {
                return;
            }

            if (outputPath == null || !Directory.Exists(outputPath))
            {
                return;
            }

            //获取当前文件所在的文件夹路径
            var fileParentPath = CommonService.GetFileParentPath(filePath);

            if (fileParentPath == null || string.IsNullOrWhiteSpace(fileParentPath))
            {
                return;
            }

            var files = Directory.GetFiles(fileParentPath);

            if (files == null || files.Length == 0)
            {
                return;
            }

            //获取当前图片的扩展名
            var fileExtension = CommonService.GetFileExtension(filePath);
            var filesList     = files.ToList();

            //按照逻辑,如果同类型图片数量小于X张,则不移动
            if (filesList.Count(item => CommonService.GetFileExtension(item).Equals(fileExtension)) < _minCount)
            {
                return;
            }

            //否则获取全部同类型的图片进行移动
            var moveList = filesList.FindAll(item => CommonService.GetFileExtension(item).Equals(fileExtension));
            //创建输出目录
            var outputTruePath = Path.Combine(outputPath, CommonService.GetFileFolder(filePath));

            if (!Directory.Exists(outputTruePath))
            {
                Directory.CreateDirectory(outputTruePath);
            }
            if (!Directory.Exists(outputTruePath))
            {
                throw new Exception($"输出目录【{ outputTruePath }】创建失败!");
            }

            //进行移动
            foreach (var item in moveList)
            {
                string fileName       = CommonService.GetFileName(item);
                var    fileOutputPath = Path.Combine(outputTruePath, fileName);
                if (!File.Exists(fileOutputPath))
                {
                    File.Move(item, fileOutputPath);
                }
            }
        }