| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using System.IO;
- using SFB;
- using System;
- public class FileToolkit
- {
- public static ExtensionFilter extensionFilter = new ExtensionFilter("图像/视频文件", "wmv", "mp4", "avi", "mov", "WMV", "MP4", "AVI", "MOV", "jpg", "png", "JPG", "PNG", "jpeg", "JPEG");
- public static List<ExtensionFilter> GetExtensionFilters()
- {
- List<ExtensionFilter> extensionFilters = new List<ExtensionFilter>();
- extensionFilters.Add(extensionFilter);
- return extensionFilters;
- }
- /// <summary>
- /// 读取本地数据并保存到指定位置
- /// </summary>
- /// <param name="fliePath"></param>
- /// <param name="targetFilePath"></param>
- public static void ReadLocalFileDataAndSaveToTargetPath(string fliePath, string targetFilePath)
- {
- byte[] buffer = GetLoaclFileData(fliePath);
- FileLocalSave(targetFilePath,buffer);
- }
- /// <summary>
- /// 获取本地文件数据
- /// </summary>
- /// <param name="filePath"></param>
- /// <returns></returns>
- public static byte[] GetLoaclFileData(string filePath)
- {
- using (FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate))
- {
- byte[] buffer = new byte[fs.Length];
- fs.Read(buffer, 0, buffer.Length);
- fs.Close();
- return buffer;
- }
- }
- /// <summary>
- /// 文件本地保存
- /// </summary>
- public static void FileLocalSave(string savePath,byte[] data)
- {
- Debug.Log(data.Length);
- using (FileStream fs = new FileStream(savePath,FileMode.OpenOrCreate,FileAccess.ReadWrite))
- {
- fs.Write(data, 0, data.Length);
- fs.Flush();
- }
- }
- /// <summary>
- /// 删除本地文件
- /// </summary>
- public static void DeleteLocalFile(string filePath)
- {
- if (File.Exists(filePath)) File.Delete(filePath);
- }
- /// <summary>
- /// 本地是否包含文件
- /// </summary>
- public static bool LoaclIsContainFile(string filePath)
- {
- return File.Exists(filePath);
- }
- /// <summary>
- /// 通过WinForm获取上传文件路径
- /// </summary>
- /// <param name="extensions"></param>
- /// <returns></returns>
- public static string[] GetAssetPathsByWinForm(List<ExtensionFilter> extensions)
- {
- string[] paths = StandaloneFileBrowser.OpenFilePanel(" 上传文件", "", extensions.ToArray(), true);
- return paths;
- }
-
- /// <summary>
- /// 获取文件的创建时间
- /// </summary>
- /// <param name="filePath"></param>
- /// <returns></returns>
- public static DateTime GetFileCreateTime(string filePath)
- {
- return File.GetCreationTime(filePath);
- }
- /// <summary>
- /// 对比文件创建前后
- /// </summary>
- /// <param name="filePath1"></param>
- /// <param name="filePath2"></param>
- /// <returns>True为FilePath1靠前</returns>
- public static bool CompareBeforAndAfterCreateTime(string filePath1,string filePath2)
- {
- DateTime dateTime1 = GetFileCreateTime(filePath1);
- DateTime dateTime2 = GetFileCreateTime(filePath2);
- return dateTime1 > dateTime2;
- }
- /// <summary>
- /// 搜索指定条件的文件,并返回文件路径
- /// </summary>
- /// <param name="targetPath"></param>
- /// <param name="searchPattern"></param>
- /// <returns></returns>
- public static string[] SearchFilePaths(string targetPath,string searchPattern)
- {
- DirectoryInfo directoryInfo = new DirectoryInfo(targetPath);
- List<string> tmpFilePaths = new List<string>();
- if (!directoryInfo.Exists)
- {
- Debug.LogError(targetPath + "路径不存在!!!!");
- }else
- {
- FileInfo[] fileInfos = directoryInfo.GetFiles(searchPattern);
- foreach (var item in fileInfos)
- {
- tmpFilePaths.Add(item.FullName);
- }
- }
- return tmpFilePaths.ToArray();
- }
- }
|