Unity 本地文件夹 目录查找,判断有否 没有创建 有删除。

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

public class shanchu : MonoBehaviour {

    string path= @"D:\\a2";

    void Start () {

    }

    void Update () {
        if (Input.GetKeyDown(KeyCode.Q))
        {
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
                print("文件夹不存在,创建");
            }
            else
            {
                DirectoryInfo file1 = new DirectoryInfo(path);
                deleteDirs(file1);
                file1 = null;
                print("文件夹存在,删除");
            }
        }
    }




    void deleteDirs(DirectoryInfo dirs)
    {
        if (dirs == null || (!dirs.Exists))
        {
            return;
        }

        DirectoryInfo[] subDir = dirs.GetDirectories();
        if (subDir != null)
        {
            for (int i = 0; i < subDir.Length; i++)
            {
                if (subDir[i] != null)
                {
                    deleteDirs(subDir[i]);
                }
            }
            subDir = null;
        }

        FileInfo[] files = dirs.GetFiles();
        if (files != null)
        {
            for (int i = 0; i < files.Length; i++)
            {
                if (files[i] != null)
                {
                    Debug.Log("删除文件:" + files[i].FullName + "__over");
                    files[i].Delete();
                    files[i] = null;
                }
            }
            files = null;
        }

        Debug.Log("删除文件夹:" + dirs.FullName + "__over");
        dirs.Delete();
    }

}

 

你可能感兴趣的:(C#,Unity,3D)