Home > Net >  Directory.CreateDirectory does not work after building Unity project
Directory.CreateDirectory does not work after building Unity project

Time:07-11

As I described in the tittle, creating a new directory with Directory.CreateDirectory does not work after I build my Unity project. Strangely, it works just fine when I launch it from the editor.

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

public class FileTool : MonoBehaviour
{
    public string folderName = "name";
    public string path = Environment.CurrentDirectory;
    public string fullPath;

    public void CreateFolder()
    {
        fullPath = Path.Combine(path, folderName);
        Debug.Log(fullPath);
        if (!Directory.Exists(fullPath))
        {
            Directory.CreateDirectory(fullPath);
        }
    }
}

CodePudding user response:

I don't think that Environment.CurrentDirectory will return something adequate in the build. Even if so, I highly doubt that you will have write permission to this location.

Use Application.persistentDataPath instead to create files in the build and store them between sessions.

CodePudding user response:

While I wasn't able to make it automatic, I managed to make it work by having the user input the name and location.

  • Related