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

            }
        }
    }
相关推荐
Akiyama_Mio-Kon10 小时前
2026 最新:Dify 本地 Docker 部署完整教程(Windows + Git Bash)
windows·git·docker
霸道流氓气质10 小时前
Spring AI 输出解析器进阶
人工智能·windows·spring
黄一一91124313 小时前
跨平台视频图片压缩互转!离线视频压缩工具!Github开源轻松无损压缩视频!Win/Mac/Linux三端通用开源工具
windows·macos·音视频·开源软件
Madokaly13 小时前
DOTween的Vector3Plugin
unity
池塘游鱼14 小时前
VMware虚拟机安装Windows 10系统 超详细图文教程
windows·vmware
易点云徐源17 小时前
(已解决)BOSS直聘 Windows 客户端一直“加载中,请稍候”怎么办?
windows
绮罗郁金香17 小时前
windows延迟更新
windows
苏生Susheng19 小时前
【软件实施】Windows 服务器运维实战
java·运维·服务器·windows·spring boot·javaweb·实施
方向研究19 小时前
UEFI/Windows启动流程
windows
ZnS_oscar19 小时前
不用下载字体,获得仿宋字体
windows·word