Unity 文件(文件夹)操作

Unity 文件(文件夹)操作

文件拷贝
  private static void CopyFile(string sourcePath,string targetPath)
    {
        FileInfo fileinfo = new FileInfo(targetPath);
        if (File.Exists(sourcePath)){
            if (!fileinfo.Directory.Exists){
                fileinfo.Directory.Create();
            }
            File.Copy(sourcePath, targetPath,true);
        }
    }
文件夹删除
    private static void DeleteDirectory(DirectoryInfo directoryInfo)
    {
        DirectoryInfo[] directoryInfos = directoryInfo.GetDirectories();
        FileInfo[] fileInfos = directoryInfo.GetFiles();
        for (int i = 0; i < directoryInfos.Length;i++ ){
            DeleteDirectory(directoryInfos[i]);
        }
        for (int i = 0; i < fileInfos.Length;i++ ){
            fileInfos[i].Delete();
        }
        directoryInfo.Delete();
    }
文件夹的创建
                        if (!Directory.Exists(Application.streamingAssetsPath))
                        {
                            Debug.Log("Directory.CreateDirectory=" + Application.streamingAssetsPath);
                            Directory.CreateDirectory(Application.streamingAssetsPath);
                        }
文件的删除
                       if (File.Exists(assetBundleListTxtPath))
                        {
                            File.Delete(assetBundleListTxtPath);
                        }
写入文件 WriteTextFile(path, “name.txt”);
    private static void WriteTextFile(string path, string text)
    {
        if (File.Exists(path))
        {
            File.Delete(path);
        }
        StreamWriter sw = File.CreateText(path);
        sw.Write(text);
        sw.Close();
        sw.Dispose();
        AssetDatabase.Refresh();
    }

//例子

private static void ChangeConfig(string serverPath, string bundleIdentifier, string newVersion)
    {
        StringBuilder ContextsCode = new StringBuilder();
        ContextsCode.AppendLine("namespace GearEngine.ApplicationContexts");
        ContextsCode.AppendLine("{");
        ContextsCode.AppendLine("    public class ApplicationContextConstants");
        ContextsCode.AppendLine("    {");
        ContextsCode.AppendLine(string.Format("        public const string SERVER_PATH = \"{0}\";", serverPath));
        ContextsCode.AppendLine("    }");
        ContextsCode.AppendLine("}");
        WriteTextFile(Application.dataPath + "/Scripts/GearEngine/ApplicationContext/ApplicationContextConstants.cs", ContextsCode.ToString().Replace("\r", ""));
    }
获取一个文件夹下面所有的文件夹或者文件
        GetAllFiles(new DirectoryInfo(filePath));
    }
    public static string filePath = Application.dataPath + "/BundleResources";
    public static void GetAllFiles(DirectoryInfo directoryInfo)
    {
        DirectoryInfo[] directoryInfos = directoryInfo.GetDirectories();
        FileInfo[] fileInfos = directoryInfo.GetFiles();
        for (int i = 0; i < directoryInfos.Length; i++)
        {
            Debug.Log("DirectoryInfo:" + directoryInfos[i].FullName);
            GetAllFiles(directoryInfos[i]);
        }
        for (int i = 0; i < fileInfos.Length; i++)
        {
            if (!fileInfos[i].FullName.Contains(".meta"))
            {
                Debug.Log("fileInfos:" + fileInfos[i].FullName);
            }
        }
    }

Copy一个文件夹下所有文件至目标文件
    public static void CopyDirectory(string srcPath, string targetPath)
    {
        try
        {
            DirectoryInfo dir = new DirectoryInfo(srcPath);
            FileSystemInfo[] fileinfo = dir.GetFileSystemInfos();  //获取目录下(不包含子目录)的文件和子目录
            foreach (FileSystemInfo i in fileinfo)
            {
                if (i is DirectoryInfo)     //判断是否文件夹
                {
                    if (!Directory.Exists(targetPath + "\\" + i.Name))
                    {
                        Directory.CreateDirectory(targetPath + "\\" + i.Name);   //目标目录下不存在此文件夹即创建子文件夹
                    }
                    CopyDirectory(i.FullName, targetPath + "\\" + i.Name);    //递归调用复制子文件夹
                }
                else
                {
                    File.Copy(i.FullName, targetPath + "\\" + i.Name, true);      //不是文件夹即复制文件,true表示可以覆盖同名文件
                }
            }
        }
        catch (Exception e)
        {
            Debug.LogError(e.ToString());
        }
    }

你可能感兴趣的:(unity,C#)