【Unity关于Windows文件的一些常用操作1】

Unity关于Windows文件的一些常用操作

本人项目中遇到的一些Unity关于Windows文件的一些常用操作。以后还会继续进行扩展。

using System;
using System.IO;
using System.Linq;
using UnityEngine;

namespace WKFrame.Controller
{
    public partial class Utils : Singleton<Utils>
    {
        /// 
        /// 清空文件夹
        /// 
        public static void ClearDirectory(string path)
        {
            if (!Directory.Exists(path)) return;

            DirectoryInfo dir = new DirectoryInfo(path);
            FileSystemInfo[] fileinfo = dir.GetFileSystemInfos();

            foreach (FileSystemInfo info in fileinfo)
            {
                if (info is FileInfo) // 判断是否为文件
                    File.Delete(info.FullName); // 删除单个文件
                else if (info is DirectoryInfo directoryInfo) // 处理子目录
                {
                    directoryInfo.Attributes &= ~FileAttributes.ReadOnly; // 移除只读属性以防无法删除
                    directoryInfo.Delete(true); // 递归删除整个子目录
                }
            }
            //Debug.Log("All files and subdirectories have been cleared from the folder at: " + path);
        }

        /// 
        /// 创建文件夹
        /// 
        public static void CreateFolder(string folderPath)
        {
            Directory.CreateDirectory(folderPath);
        }

        /// 
        /// 判断指定文件夹是否存在
        /// 
        public static bool ExistFolder(string folderPath)
        {
            return Directory.Exists(folderPath);
        }

        /// 
        /// 返回所在文件夹全路径(寻找最后一个出现方向符号的位置索引来完成切割动作从而分离头部部分)
        /// 
        public static string ExtractDirectoryNameFromFullPath(string fullPath)
        {
            int lastSeparatorIndex = fullPath.LastIndexOfAny(new char[] { '/', '\\' });

            if (lastSeparatorIndex != -1 && lastSeparatorIndex < fullPath.Length - 1)
            {
                return fullPath.Substring(0, lastSeparatorIndex);
            }

            return "";
        }

        /// 
        /// 根据文件名获取文件全路径
        /// 
        public static string GetFullPathByName(string path, string fileName)
        {
            string[] paths = Directory.GetFiles(path);
            for (int i = 0; i < paths.Length; i++)
            {
                Debug.Log(Path.GetFileNameWithoutExtension(paths[i]));
                if (Path.GetFileNameWithoutExtension(paths[i]) == fileName)
                {
                    return paths[i];
                }
            }
            return "";
        }

        /// 
        /// 获取文件夹中最后一个文件路径
        /// 
        public static string GetLastPath(string path)
        {
            string[] paths = Directory.GetFiles(path);
            return paths[paths.Length - 1];
        }

        /// 
        /// 判断某个文件夹是否为空
        /// 
        public static bool IsEmptyFolder(string folderPath)
        {
            //文件夹是否存在
            if (Directory.Exists(folderPath))
            {
                //获取文件夹中的所有文件
                string[] files = Directory.GetFiles(folderPath);
                //获取文件夹中的所有子文件夹
                string[] subdirectories = Directory.GetDirectories(folderPath);
                bool isEmpty = files.Length == 0 && subdirectories.Length == 0;
                return isEmpty;
            }
            else
            {
                return false;
            }
        }

        /// 
        /// 判断某个文件是否存在
        /// 
        public static bool IsExistFile(string filePath)
        {
            //文件夹路径
            string folderPath = ExtractDirectoryNameFromFullPath(filePath);
            //文件夹是否存在
            if (Directory.Exists(folderPath))
            {
                bool isEmpty = !Directory.EnumerateFileSystemEntries(folderPath).Any();
                return isEmpty;
            }
            else
            {
                return false;
            }
        }

        /// 
        /// 判断指定文件夹   在桌面上是否存在
        /// 
        public static bool IsExistFolderInDesktop(string folderName)
        {
            string fullPath = Path.Combine(Constant.DesktopPath, folderName);
            return Directory.Exists(fullPath);
        }

        /// 
        /// 创建指定文件夹--在桌面
        /// 
        public static void CreateAimFolderInDesktop(string folderName)
        {
            string fullPath = Path.Combine(Constant.DesktopPath, folderName);
            Directory.CreateDirectory(fullPath);
        }

        /// 
        /// 重命名文件
        /// 
        public static void RenameFile(string oldFilePath, string newFilePath)
        {
            try
            {
                if (File.Exists(oldFilePath))
                {
                    File.Move(oldFilePath, newFilePath);
                    Debug.Log("文件重命名成功");
                }
                else
                {
                    Debug.LogWarning("原文件不存在");
                }
            }
            catch (System.Exception ex)
            {
                Debug.LogError("重命名失败: " + ex.Message);
            }
        }

        /// 
        /// 交换文件名(2个)
        /// 
        public static void SwapNameFile(string oldFilePath, string newFilePath)
        {
            try
            {
                if (File.Exists(oldFilePath))
                {
                    //备份旧名字
                    string nextName = newFilePath;
                    string oldName = oldFilePath;
                    string tempName = oldFilePath + "temp";
                    //旧名字修改为新的临时名字,防止文件重名
                    File.Move(oldFilePath, tempName);
                    if (File.Exists(newFilePath))
                    {
                        //先重命名另一个文件
                        File.Move(newFilePath, oldName);
                        //再把自身从临时名改为另一个文件的名字
                        File.Move(tempName, nextName);
                    }
                    else
                    {
                        Debug.LogWarning("另一个原文件不存在");
                    }
                }
                else
                {
                    Debug.LogWarning("原文件不存在");
                }
            }
            catch (System.Exception ex)
            {
                Debug.LogError("重命名失败: " + ex.Message);
            }
        }
    }
}

你可能感兴趣的:(Unity小技巧,unity,游戏引擎)