unity 文件和文件夹的创建、删除

1、在程序根目录创建

   using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using UnityEditor;

public class FileDelete : MonoBehaviour
{
    private string DeletePath;
    void Start()
    {
        //创建文件夹路径
        DeletePath = Application.dataPath + "/video";
        Debug.Log(DeletePath);
        //判断文件夹路径是否存在
        if (!Directory.Exists(DeletePath))
        {
            //创建
            Directory.CreateDirectory(DeletePath);
        }
        //刷新
        AssetDatabase.Refresh();
    }

}

在这里插入图片描述

2、在上级目录创建文件夹

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using UnityEditor;

public class FileDelete : MonoBehaviour
{
    private string DeletePath;
    void Start()
    {
        //创建文件夹路径(/../代表Application.dataPath 路径的上一级目录,/../../表示上两级目录)
        DeletePath = Application.dataPath + "/../" + "video";
        Debug.Log(DeletePath);
        if (!Directory.Exists(DeletePath))
        {
            Directory.CreateDirectory(DeletePath);
        }

    }
}

在这里插入图片描述
3、文件的创建(创建path.txt)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using UnityEditor;

public class FileDelete : MonoBehaviour
{
    private string DeletePath;
    private string DeletePaths;
    void Start()
    {
        //创建文件夹路径(/../代表Application.dataPath 路径的上一级目录)
        DeletePath = Application.dataPath + "/../" + "video";
        DeletePaths = DeletePath + "/path.txt";
        Debug.Log(DeletePath);
        if (!Directory.Exists(DeletePath))
        {
            Directory.CreateDirectory(DeletePath);
        }
        FileInfo fileInfo = new FileInfo(DeletePaths);
        if(!fileInfo.Exists)
        {
            fileInfo.CreateText();
        }
    }
}

4、删除文件夹下的所有文件夹和文件

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using UnityEditor;
using System;

public class FileDelete : MonoBehaviour
{
    private string DeletePath;
    private string DeletePaths;

    void Start()
    {
        DeletePath = Application.dataPath + "/../" + "Video";
        if (!Directory.Exists(DeletePath))
        {
            Directory.CreateDirectory(DeletePath);
        }
        try
        {
            DirectoryInfo dir = new DirectoryInfo(DeletePath);
            FileSystemInfo[] files = dir.GetFileSystemInfos();
            foreach (FileSystemInfo item in files)
            {
                if (item is DirectoryInfo)//判断是否文件夹
                {
                    DirectoryInfo subdir = new DirectoryInfo(item.FullName);
                    subdir.Delete(true);//删除子目录和文件
                }
                else
                {
                    File.Delete(item.FullName);//删除指定文件
                }
            }
        }
        catch (Exception e)
        {
            throw new Exception(e.Message);
        }

    }
}
  • 6
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值