unity指定路径读写

 


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

public class FileManager
{

    /// 
    /// 获取path目录下所有的filesExtension文件,返回List
    /// 
    /// 
    public static List GetFileInfo()
    {
        int fileLength = 0;
        List pathList = new List();
        string path = Application.streamingAssetsPath;
        if (Directory.Exists(path))
        {
            DirectoryInfo direction = new DirectoryInfo(path);
            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;
                }

                if (files[i].Extension.ToLower().Contains("mp4"))
                {
                    string name = Path.GetFileNameWithoutExtension(files[i].ToString());
                    Debug.Log(name);
                    pathList.Add(path + "/" + name + files[i].Extension);
                    fileLength++;
                }

            }
            if (fileLength > 0)
            {
                Debug.Log("文件数量:" + fileLength);
                //do something
            }
            else
            {
                Debug.Log("文件夹下数据为空!");
            }
        }
        else
        {
            Debug.Log(path + "不存在!");
            Directory.CreateDirectory(path);
        }
        return pathList;
    }

    /// 
    /// 获取path目录下所有的filesExtension文件,返回string
    /// 
    /// 
    /// 
    /// 
    public static string GetFileInfo(string folderName, string filesExtension)
    {
        int fileLength = 0;
        string pathList = "";
        string path = Path.Combine(Application.streamingAssetsPath, folderName);
        //Debug.Log(Directory.Exists(path));
        if (Directory.Exists(path))
        {
            DirectoryInfo direction = new DirectoryInfo(path);
            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;
                }
                if (files[i].Extension.ToLower().Contains(filesExtension))
                {
                    string name = Path.GetFileNameWithoutExtension(files[i].ToString());
                    Debug.Log(name);
                    pathList = path + "/" + name + files[i].Extension;
                    fileLength++;
                }
            }
            if (fileLength > 0)
            {
                Debug.Log("文件数量:" + fileLength);
                //do something
            }
            else
            {
                Debug.Log(folderName + "文件夹下数据为空!");
            }
        }
        else
        {
            Debug.Log(path + "不存在!");
            Directory.CreateDirectory(path);
        }
        return pathList;
    }

}

 

你可能感兴趣的:(.Net,framework,unity)