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

            }
        }
    }
相关推荐
Abigail_chow5 小时前
EXCEL如何快速批量给两字姓名中间加空格
windows·microsoft·excel·学习方法·政务
海尔辛7 小时前
Unity UI 性能优化--Sprite 篇
ui·unity·性能优化
love530love7 小时前
【笔记】在 MSYS2(MINGW64)中正确安装 Rust
运维·开发语言·人工智能·windows·笔记·python·rust
代码搬运媛8 小时前
“packageManager“: “[email protected]“ 配置如何正确启动项目?
windows·webpack
小道士写程序9 小时前
Qt 5.12 上读取 .xlsx 文件(Windows 平台)
开发语言·windows·qt
三巧12 小时前
Godot 敌人生成半径和围墙不匹配,导致敌人错误的生成在围墙外的解决代码
游戏引擎·godot
技术小甜甜12 小时前
【Godot引擎】如何使用内置的全局搜索功能提升开发效率
游戏引擎·godot
技术小甜甜12 小时前
【Godot】如何导出 Release 版本的安卓项目
android·游戏引擎·godot
异常君14 小时前
Windows 与 Linux 虚拟内存机制对比:设计理念与实现差异
java·linux·windows
搏博16 小时前
将图形可视化工具的 Python 脚本打包为 Windows 应用程序
开发语言·windows·python·matplotlib·数据可视化