unity对于文件夹的操作

1、获取目标文件夹内所有文件夹

csharp 复制代码
 string[] directories = Directory.GetDirectories(Path);

                for (int i = 0; i < directories.Length; i++)
                {
                    print(directories[i]);
                }
//只要文件夹名字,没有路径
foreach (DirectoryInfo item in new DirectoryInfo(StreamingAssetsPath).GetDirectories())
        {
            file_List.Add(item.Name);
        }
//获取文件夹和文件
string[] directoryEntries = Directory.GetFileSystemEntries(StreamingAssetsPath);

2、获取目标文件夹内指定文件

csharp 复制代码
 public List<string> GetAllTxt(string path)
    {
        //只获取文件名
        string[] files = Directory.GetFiles(path, "*.txt");

        List<string> listfiles = new List<string>();

        foreach (string file in files)
        {
            string[] lis = Path.GetFileName(file).Split('.');

            listfiles.Add(lis[0]);
            // 输出文件名
           // Debug.Log(Path.GetFileName(file));
        }

        return listfiles;
    }

3、移动文件到指定文件夹

csharp 复制代码
 // 源文件路径  
        string sourceFile = @"C:\sourceFolder\myfile.txt";  
          
        // 目标文件路径(包括目标文件夹)  
        string destFile = @"C:\destinationFolder\myfile.txt";  
  
        try  
        {  
            // 检查源文件是否存在  
            if (File.Exists(sourceFile))  
            {  
                // 移动文件  
                File.Move(sourceFile, destFile);  
                Console.WriteLine("文件已从 {0} 移动到 {1}", sourceFile, destFile);  
            }  
            else  
            {  
                Console.WriteLine("源文件不存在: {0}", sourceFile);  
            }  
        }  
        catch (Exception ex)  
        {  
            // 捕获并处理可能出现的异常  
            Console.WriteLine("移动文件时发生错误: " + ex.Message);  
        }  

4、获取文件创建时间

csharp 复制代码
                FileInfo fileInfo = new FileInfo(path);
                DateTime dt = fileInfo.CreationTime;

FileInfo一般被用来获取文件信息

5、创建文件夹

csharp 复制代码
 //判断文件夹是否存在
                if (!Directory.Exists(path))
                {
                    //若不存在则创建
                    Directory.CreateDirectory(path);
                }
csharp 复制代码
//移动到指定文件夹 路径要加文件名
 fileInfo.MoveTo(path);

6、写入txt和读取txt

csharp 复制代码
public void Read(string str)
    {
        if (!File.Exists(Application.streamingAssetsPath + "/" + str + ".txt"))
        {
            return;
        }


       shuzu= File.ReadAllLines(Application.streamingAssetsPath + "/" + str + ".txt");

        for (int i = 0; i < shuzu.Length; i++)
        {
            print(shuzu[i]);
        }
    }
csharp 复制代码
 public bool Write_Bool(string path,string str,string[] content)
    {
        if (!File.Exists(path + "/" + str + ".txt"))
        {
            FileStream fileStream = new FileStream(path + "/" + str + ".txt", FileMode.OpenOrCreate);
            fileStream.Close();
        }
        else
        {
            return false;
        }

       
        File.WriteAllLines(path + "/" + str + ".txt", content);
        return true;
    }
    //写入文件内容返回bool
    public bool Write_Bool(string path, string str, byte[] content)
    {
        if (!File.Exists(path + "/" + str + ".txt"))
        {
            FileStream fileStream = new FileStream(path + "/" + str + ".txt", FileMode.OpenOrCreate);
            fileStream.Close();
        }
        else
        {
            return false;
        }


        File.WriteAllBytes(path + "/" + str + ".txt", content);
        return true;
    }

注:如果在执行完Read后要修改该文件,可以之间在后面加上File.WriteAllLines,若执行Write_Bool则会因为Read占用此文件而报错。

安卓平台读取txt逐行读取

csharp 复制代码
IEnumerator DownloadFiletxt(string url, string fileName)
    {
        using (UnityWebRequest uwr = UnityWebRequest.Get(url))
        {
            yield return uwr.SendWebRequest();

            if (uwr.result == UnityWebRequest.Result.ConnectionError || uwr.result == UnityWebRequest.Result.ProtocolError)
            {
                Single._instan.OnError(uwr.error);
                Debug.LogError(uwr.error);
            }
            else
            {
                // 下载成功,获取数据  
                byte[] data = uwr.downloadHandler.data;
                
                //获取路径txt里面的服务器路径
                string str = System.Text.Encoding.UTF8.GetString(data);

                using(StringReader reader=new StringReader(str))
                {
                    string line;
                    while ((line = reader.ReadLine()) != null)
                    {
                        Single._instan.OnError(line);
                    }
                }

            }
        }
    }
相关推荐
美酒没故事°17 小时前
Open WebUI安装指南。搭建自己的自托管 AI 平台
人工智能·windows·ai
黄思搏18 小时前
基于标注平台数据的 Unity UI 自动化构建工作流设计与工程实践
ui·unity·蓝湖·vectoui
一个欠登儿程序员19 小时前
在国产服务器上通过 Docker 部署 Windows 虚拟机
服务器·windows·docker
爱宇阳20 小时前
WSL2 隔离 Windows PATH 实战指南
windows·环境变量
ALex_zry20 小时前
C++模板元编程实战技巧
网络·c++·windows
I疯子20 小时前
2026-04-08 打卡第 5 天
开发语言·windows·python
一个人旅程~21 小时前
旧笔记本电脑安装win10精简版LTSB&win10LTSC&linuxmint作为三系统的操作指导书(以DELL n4020为例)
linux·windows·经验分享·电脑
开开心心就好21 小时前
支持自定义名单的实用随机抽签工具
windows·计算机视觉·计算机外设·excel·散列表·启发式算法·csdn开发云
dyj0951 天前
OpenClaw小龙虾本地部署【Windows系统 + 接入飞书】
windows·飞书
CresCent_Charles1 天前
(已解决)踩坑记录:Windows 11安装pointops编译时报错
windows