- Home /
Check if folder exists - Unity's way
We have the FileUtil class that lets you do move, copy, delete operations over files or directories.
We have the AssetDatabase class that lets you create a new folder.
Is there any class that lets you check if a folder exists? Or should I use C# **Directory.Exists**?
Is there any reason you are opposed to using Directory.Exists? That's what I've always used in my editor scripts.
What seems missing here, though, is any way to map between an asset directory in Unity and folders in the actual filesystem (and in a platform-independent way). Yes, I know...you could hard-code the path in your script, but that's just gross... :/
I haven't had any problem mapping the file system to the asset directory using Application.dataPath.
Answer by jahroy · Oct 18, 2011 at 08:04 PM
My guess is functions like AssetDatabase.CreateFolder were implemented because adding a file to a Unity project is different than creating a normal file. There is probably some overhead involved (like registering it with the project or saving its state).
In contrast, checking whether a file exists is the same in all environments (whether you're writing a Unity application or not). My theory is this is why there is no need to create a "Unity version" of the function that checks if a file exists.
I had the same question a week ago, and that's how I've rationalized what I've found (or not found) in the documentation.
Edit - Just came acros this in the docs:
File Operations using the AssetDatabase
Since Unity keeps metadata about asset files, you should never create, move or delete them using the filesystem.
Instead, you can use AssetDatabase.Contains, AssetDatabase.CreateAsset, AssetDatabase.CreateFolder, AssetDatabase.RenameAsset, AssetDatabase.CopyAsset, AssetDatabase.MoveAsset, AssetDatabase.MoveAssetToTrash and AssetDatabase.DeleteAsset.
Answer by e4lime · Mar 06, 2015 at 12:22 PM
You can now do:
AssetDatabase.IsValidFolder(string path);
Now does this let us know whetther a file (not a folder) exists in this path ?
Of course not. The question was about checking if a folder exists. And the method name "IsValidFolder" clearly only returns true when the given path "is a valid folder"
Your answer

Follow this Question
Related Questions
How to access persistent data path in Android phone? 3 Answers
Read and Write to text file Online 4 Answers
File.IO WriteAllText not working? 1 Answer
Opening a file explorer in run time? 8 Answers