I have a script to make a text file, but if the folder does not exist, it won’t create the text file.
3 Answers
3As 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 ![]()
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
Anything related to file IO is always in System.IO.
It worked, Thanks.
– MH1@vexe Nicely Explained. Thanks!!
– charmi212121thanks dude !!!
– Bohdan-ShpytkoI wonder why unity didn't put this as a built-in method? or did they?
– ardiawanbagusharisawhy did you define a var to create a folder instead of just calling the CreateDirectory function on it's own? Is there any use for that? Just writing
– guitarjorge24Directory.CreateDirectory("path/to/your/dir");should've been enough