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);
                    }
                }

            }
        }
    }
相关推荐
虾球xz1 小时前
游戏引擎学习第119天
学习·游戏引擎
不爱学习的YY酱3 小时前
MusicGPT的本地化部署与远程调用:让你的Windows电脑成为AI音乐工作站
人工智能·windows
web150854159354 小时前
超级详细Spring AI运用Ollama大模型
人工智能·windows·spring
lovingyk5 小时前
Unity 脚本控制3D人物模型的BlendShape
unity
久绊A8 小时前
Python 基本语法的详细解释
开发语言·windows·python
晴空了无痕9 小时前
现代任务调度系统架构深度解析——以TaskSchedulerController为核心的弹性任务管理方案
unity·系统架构·游戏引擎
虾球xz16 小时前
游戏引擎学习第118天
学习·游戏引擎
软件黑马王子17 小时前
Unity游戏制作中的C#基础(6)方法和类的知识点深度剖析
开发语言·游戏·unity·c#
菜鸟单飞18 小时前
介绍一款非常实用的PDF阅读软件!
windows·pdf·电脑
不吃斋的和尚1 天前
Unity中一个节点实现植物动态(Shader)
unity·游戏引擎