• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
6
Question by MH1 · Sep 02, 2013 at 01:48 AM · c#textfilecreatefolder

How do you create a folder in c#?

I have a script to make a text file, but if the folder does not exist, it won't create the text file.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
28
Best Answer

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 :)

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image MH1 · Sep 03, 2013 at 01:19 AM 0
Share

It worked, Thanks.

avatar image charmi212121 · Feb 09, 2016 at 06:11 PM 0
Share

@vexe Nicely Explained. Thanks!!

avatar image Bohdan-Shpytko · Jul 17, 2016 at 09:12 AM 0
Share

thanks dude !!!

avatar image ardiawanbagusharisa · Jan 15, 2018 at 03:10 AM 0
Share

I wonder why unity didn't put this as a built-in method? or did they?

avatar image Eric5h5 ardiawanbagusharisa · Jan 15, 2018 at 03:33 AM 0
Share

Unity uses Mono, System.IO is part of Mono, 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.)

avatar image guitarjorge24 · Feb 18 at 08:35 AM 0
Share

why 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 Directory.CreateDirectory("path/to/your/dir"); should've been enough

avatar image
6

Answer by Eric5h5 · Sep 02, 2013 at 02:24 AM

Anything related to file IO is always in System.IO.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
6

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

26 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Why do I get the error - 'WriteAllText' is not a member of 'System.IO.File 2 Answers

[Android] Open .txt file from c# 1 Answer

Trouble creating a text file 4 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges