unity 判断路径是否存在或者文件夹是否存在

目录

一.目的

1.想知道:unity 判断路径是否存在或者文件夹是否存在

1.想实现的功能:某路径下,检查是否有名字为“1”-“20”名字的文件夹。

二.参考

1.Unity api

1.unity 判断路径是否存在或者文件夹是否存在

1.如何检查文件是否存在于unity android中?(How to check a file exist or not in unity android?)

1.Unity安卓配置文件:读和写

1.unity 通过代码查找一个文件夹下的所有文件

三.注意:Directory.Exists和File.Exists是有去别的,

1.经过多次测试:只能 Application.persistentDataPath  和File.Exists(str_filePath)只能发现指定类型的文件,而无法发现某名字的文件夹。

四.操作:完成:获取指定路径下面是否存在名字为“1”的文件夹。

1.可以获取指定路径下面是否存在名字为“1”的文件夹。

三.操作:完成:文件夹不存在就创建


一.目的

1.想知道:unity 判断路径是否存在或者文件夹是否存在

 

1.想实现的功能:某路径下,检查是否有名字为“1”-“20”名字的文件夹。

 

二.参考

1.Unity api

https://docs.unity3d.com/ScriptReference/Windows.Directory.html

  • 总结:good:适合多看

 

1.unity 判断路径是否存在或者文件夹是否存在

https://blog.csdn.net/mo_qi_qi/article/details/108827106

  • 总结:good:判断指定路径内是否有指定文件夹-System.IO.Directory.Exists("路径")
  • 总结:good:判断指定路径内是否有指定文件- System.IO.File.Exists(“路径”)

 

  • 总结:下面是我自己操作:是成功的。

        //测试:12:获取指定路径下面文件夹是否存在:待检测
        string tmpStr_fileName = "1";
        string str_filePath = Application.persistentDataPath + "/"+ "MyRecord" + "/"+ tmpStr_fileName;
        Debug.LogError("Unity xzy :检查的文件夹路径:" + str_filePath);

        //获取指定路径下面的所有资源文件  
        if (Directory.Exists(str_filePath))
        {
            Debug.LogError("Unity xzy :文件夹 1  已经存在:路径是" + str_filePath);
        }
        else
        {
            Debug.LogError("Unity xzy :文件夹 1  不存在:");
        }

        return tmpStr_fileName;

 

 

1.如何检查文件是否存在于unity android中?(How to check a file exist or not in unity android?)

https://www.it1352.com/1850161.html

  • 总结:失败

 

 

1.Unity安卓配置文件:读和写

https://blog.csdn.net/qq_40544338/article/details/102570247

  • 总结:good:可以实现

 

 

1.unity 通过代码查找一个文件夹下的所有文件

https://blog.csdn.net/huhudeni/article/details/77804118

  • 总结:good:就是我所需要的;因为Application.persistentDataPath  和File.Exists(str_filePath)只能发现指定类型的文件,而无法发现某名字的文件夹。所以想通过查找所有文件的方法,判断这个文件夹里面是否有指定文件。

 

  • 总结:Name找到文件的名字。
  • 总结:FullName找到文件整个路径
  • 总结:DirectoryName找到文件所在的绝对路径。

        //测试:10:获取指定路径下面文件夹是否存在:成功
        string tmpStr_fileName = "";
        string str_filePath = Application.persistentDataPath + "/";
        Debug.LogError("Unity xzy :检查的文件夹路径:" + str_filePath);

        //获取指定路径下面的所有资源文件  
        if (Directory.Exists(str_filePath))
        {
            DirectoryInfo direction = new DirectoryInfo(str_filePath);
            FileInfo[] files = direction.GetFiles("*", SearchOption.AllDirectories);

            Debug.Log(files.Length);

            for (int i = 0; i < files.Length; i++)
            {
                if (files[i].Name.EndsWith(".meta"))
                {
                    continue;
                }
                Debug.LogError("Unity xzy :Name:" + files[i].Name);
                Debug.LogError("Unity xzy :FullName:" + files[i].FullName);
                Debug.LogError("Unity xzy :DirectoryName:" + files[i].DirectoryName);
            }
        }

 

三.注意:Directory.Exists和File.Exists是有去别的,

1.经过多次测试:只能 Application.persistentDataPath  和File.Exists(str_filePath)只能发现指定类型的文件,而无法发现某名字的文件夹。

 

 

四.操作:完成:获取指定路径下面是否存在名字为“1”的文件夹。

1.可以获取指定路径下面是否存在名字为“1”的文件夹。

 

        //测试:12:获取指定路径下面文件夹是否存在:待检测
        string tmpStr_fileName = "1";
        string str_filePath = Application.persistentDataPath + "/"+ "MyRecord" + "/"+ tmpStr_fileName;
        Debug.LogError("Unity xzy :检查的文件夹路径:" + str_filePath);

        //获取指定路径下面的所有资源文件  
        if (Directory.Exists(str_filePath))
        {
            Debug.LogError("Unity xzy :文件夹 1  已经存在:路径是" + str_filePath);
        }
        else
        {
            Debug.LogError("Unity xzy :文件夹 1  不存在:");
        }

        return tmpStr_fileName;

 

三.操作:完成:文件夹不存在就创建

        测试:12:获取指定路径下面文件夹是否存在:完成
        string tmpStr_fileName = "";

        //TODO:第 1 步:检查是否存在0-19文件夹:完成
        for (int i = 0; i <nCreateRecordDirectoryNum; i++)
        {
            tmpStr_fileName = i.ToString();
            string str_filePath = Application.persistentDataPath + "/" + strRecordFileName+ "/" + tmpStr_fileName;
            Debug.LogError("Unity xzy :检查的文件夹路径:" + str_filePath);

            //获取指定路径下面的所有资源文件  
            if (Directory.Exists(str_filePath))
            {
                Debug.LogError("Unity xzy :文件夹 :"+i+":已经存在:路径是" + str_filePath);
            }
            else
            {
                Debug.LogError("Unity xzy :文件夹 :" + i + ":不存在,然后创建" );

                //TODO:第 2 步:0-19哪个文件夹不存在,创建此文件夹,并且从此文件夹里面开始添加录制视频
                Directory.CreateDirectory(str_filePath);
            }
        }

 

  • 7
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值