unity对于文件夹的操作

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

 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、获取目标文件夹内指定文件

 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、移动文件到指定文件夹

 // 源文件路径  
        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、获取文件创建时间

                FileInfo fileInfo = new FileInfo(path);
                DateTime dt = fileInfo.CreationTime;

FileInfo一般被用来获取文件信息
5、创建文件夹

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

6、写入txt和读取txt

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]);
        }
    }
 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逐行读取

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

            }
        }
    }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值