unity文件操作FileOperatorTool

本文介绍了一个使用C#编写的文件操作工具类,包括文件和文件夹的打开、保存、删除、复制等功能,适用于Windows环境下的文件管理任务。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

using System.Collections;
using System.Collections.Generic;
using System.IO;
using System;
using System.Windows.Forms;
using UnityEngine;

namespace UIFrame
{

    public class FileOperatorTool
    {

        /// <summary>
        /// 打开windows open窗口
        /// </summary>
        /// <param name="_formTitle"></param>
        /// <param name="_filterLimit"></param>
        public static OpenFileDialog OpenFileForm(string _formTitle, string _filterLimit)
        {
            OpenFileDialog op = new OpenFileDialog();
            op.Title = _formTitle;
            op.Multiselect = true;
            op.Filter = _filterLimit;
            if (op.ShowDialog() == DialogResult.OK)
            {
                UnityEngine.Debug.Log("file name === " + op.FileName);
                //添加上传方法
                return op;
            }
            return op;
        }

        public static void OpenFolderBrowserDialog(string _formTitle, List<string> m_files)
        {
            FolderBrowserDialog dialog = new FolderBrowserDialog();
            dialog.Description = _formTitle;
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                m_files.Clear();
                string foldPath = dialog.SelectedPath;
                if (Directory.Exists(foldPath))
                {
                    string[] file_names = Directory.GetFiles(foldPath);
                    for (int i = 0; i < file_names.Length; i++)
                    {
                        m_files.Add(file_names[i]);
                    }
                }

                //ListFiles(new DirectoryInfo(foldPath), m_files);
                //ProcFiles(m_files);
            }
        }

        /// <summary>
        /// 打开windows save窗口
        /// </summary>
        /// <param name="_formTitle"></param>
        /// <param name="_filterLimit"></param>
        public static void SaveFileForm(string _formTitle, string _filterLimit)
        {
            SaveFileDialog sd = new SaveFileDialog();
            sd.Title = _formTitle;
            sd.Filter = _filterLimit;
            if (sd.ShowDialog() == DialogResult.OK)
            {
                UnityEngine.Debug.Log("unload");
            }
        }

        /// <summary>
        /// 是否存在该文件夹
        /// </summary>
        /// <param name="_dirPath">文件夹路径</param>
        /// <returns></returns>
        public static bool IsExitDir(string _dirPath)
        {
            bool isExit = false;
            if (string.IsNullOrEmpty(_dirPath))
            {
                Debug.Log("当前文件夹路径为空,_dirPath= " + _dirPath);
                isExit = false;
            }
            else
            {
                if (!Directory.Exists(_dirPath))
                {
                    Debug.Log("当前路径无该文件夹,检查_dirPath= " + _dirPath);
                    isExit = false;
                }
                else
                {
                    isExit = true;
                }
            }
            return isExit;
        }

        /// <summary>
        /// 是否存在该文件
        /// </summary>
        /// <param name="_filePath">文件路径</param>
        /// <returns></returns>
        public static bool IsExitFile(string _filePath)
        {
            bool isExit = false;
            if (string.IsNullOrEmpty(_filePath))
            {
                Debug.Log("当前文件夹路径为空,_filePath= " + _filePath);
                isExit = false;
            }
            else
            {
                if (!File.Exists(_filePath))
                {
                    Debug.Log("当前路径无该文件夹,_filePath= " + _filePath);
                    isExit = false;
                }
                else
                {
                    isExit = true;
                }
            }
            return isExit;
        }

        /// <summary>
        /// 返回当前文件夹的第一个文件名
        /// </summary>
        /// <param name="_dirPath"></param>
        /// <returns></returns>
        public static string GetDirFirstFileName(string _dirPath)
        {
            string firstNum = "";
            if (string.IsNullOrEmpty(_dirPath))
            {
                Debug.Log("当前文件夹路径为空,_dirPath= " + _dirPath);
            }
            else
            {
                if (!Directory.Exists(_dirPath))
                {
                    Debug.Log("当前路径无该文件夹,检查_dirPath= " + _dirPath);
                }
                else
                {
                    string[] fileName = Directory.GetFiles(_dirPath);
                    string firstFileName = Path.GetFileNameWithoutExtension(fileName[0]);
                    firstNum = firstFileName;
                }
            }
            return firstNum;
        }

        /// <summary>
        /// 返回当前文件夹的文件数量
        /// </summary>
        /// <param name="_dirPath"></param>
        /// <returns></returns>
        public static int GetDirFilesCount(string _dirPath)
        {
            int totalNum = 0;
            if (string.IsNullOrEmpty(_dirPath))
            {
                Debug.Log("当前文件夹路径为空,_dirPath= " + _dirPath);
            }
            else
            {
                if (!Directory.Exists(_dirPath))
                {
                    Debug.Log("当前路径无该文件夹,检查_dirPath= " + _dirPath);
                }
                else
                {
                    string[] fileName = Directory.GetFiles(_dirPath);
                    totalNum = fileName.Length;
                }
            }
            return totalNum;
        }

        /// <summary>
        /// 删除文件夹中的所有文件类型
        /// </summary>
        /// <param name="_dirPth"></param>
        public static void DeletDirectory(string _dirPth)
        {
            if (Directory.Exists(_dirPth))
            {
                //删除files
                string[] files = Directory.GetFiles(_dirPth);
                string[] subDir = Directory.GetDirectories(_dirPth);
                if (files.Length > 0)
                {
                    foreach (string item in files)
                    {
                        File.Delete(item);
                    }
                }
                //删除sub directory
                if (subDir.Length > 0)
                {
                    foreach (string item in subDir)
                    {
                        if (Directory.Exists(item))
                        {
                            DeletDirectory(item);
                        }
                    }
                }
            }
            else
            {
                Debug.Log("文件夹路径出错,检查路径_dirPth = " + _dirPth);
            }
        }

        /// <summary>
        /// 删除整个文件夹
        /// </summary>
        /// <param name="_dirPth"></param>
        public static void DeletDir(string _dirPth)
        {
            if (Directory.Exists(_dirPth))
            {
                Directory.Delete(_dirPth, true);
            }
        }

        /// <summary>
        /// 删除文件夹中指定文件名的文件
        /// </summary>
        /// <param name="_dirPath">文件夹名</param>
        /// <param name="_specifiedName">指定文件名</param>
        public static void DeletSpecifiedFile(string _dirPath,string _specifiedName)
        {
            //string[] spe_paths = Directory.GetFiles(_dirPath, _specifiedName, SearchOption.AllDirectories);
            if (Directory.Exists(_dirPath))
            {
                string[] spe_paths = Directory.GetFiles(_dirPath);
                foreach (string item in spe_paths)
                {
                    if (File.Exists(item) && item.Contains(_specifiedName))
                    {
                        File.Delete(item);
                    }
                }
            }
            
        }

        /// <summary> 
        /// 拷贝文件夹 
        /// </summary> 
        /// <param name="srcdir"></param> 
        /// <param name="desdir"></param> 
        public static void CopyDirectory(string srcdir, string desdir)
        {
            string folderName = srcdir.Substring(srcdir.LastIndexOf("\\") + 1);
            string desfolderdir = desdir + "\\" + folderName;
            if (desdir.LastIndexOf("\\") == (desdir.Length - 1))
            {
                desfolderdir = desdir + folderName;
            }
            string[] filenames = Directory.GetFileSystemEntries(srcdir);
            foreach (string file in filenames)// 遍历所有的文件和目录 
            {
                if (Directory.Exists(file))// 先当作目录处理如果存在这个目录就递归Copy该目录下面的文件 
                {
                    string currentdir = desfolderdir + "\\" + file.Substring(file.LastIndexOf("\\") + 1);
                    if (!Directory.Exists(currentdir))
                    {
                        Directory.CreateDirectory(currentdir);
                    }
                    CopyDirectory(file, desfolderdir);
                }
                else // 否则直接copy文件 
                {
                    string srcfileName = file.Substring(file.LastIndexOf("\\") + 1);
                    srcfileName = desfolderdir + "\\" + srcfileName;
                    if (!Directory.Exists(desfolderdir))
                    {
                        Directory.CreateDirectory(desfolderdir);
                    }
                    File.Copy(file, srcfileName);
                }
            }
        }

    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值