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