- Home /
Answer by vexe · Sep 02, 2013 at 02:43 AM
As Eric5h5 said, you have to first:
using System.IO
Then, if you want to create a Folder:
var folder = Directory.CreateDirectory("path/to/your/dir"); // returns a DirectoryInfo object
if you wanna create a file:
var file = File.Create("path/to/your/file"); // returns a FileInfo object
I have a nice tutorial package especially for the System.IO namespace, you'll learn everything you need about handling files and folders, have a look :)
I wonder why unity didn't put this as a built-in method? or did they?
Unity uses $$anonymous$$ono, System.IO is part of $$anonymous$$ono, it's already built-in and there's not much point duplicating something that already exists. (Not quite no point, however, since UnityEngine.Windows.Directory.CreateDirectory does exist, but only for UWP, probably because that platform has weird .NET incompatibilities.)
why did you define a var to create a folder ins$$anonymous$$d of just calling the CreateDirectory function on it's own? Is there any use for that? Just writing Directory.CreateDirectory("path/to/your/dir");
should've been enough
Answer by justinhimt · Nov 12, 2014 at 06:51 AM
Because of the static nature of Directory class , we do not have to instantiate the class. We can call the methods directly from the Directory class itself.
try
{
if (!Directory.Exists(filePath))
{
Directory.CreateDirectory(filePath);
}
}
catch (IOException ex)
{
Console.WriteLine(ex.Message)
}
Source : Directory Class
Justin
Your answer
Follow this Question
Related Questions
Loading TextAsset returns null 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
[Android] Open .txt file from c# 1 Answer
Why do I get the error - 'WriteAllText' is not a member of 'System.IO.File 2 Answers