• 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
8
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
36
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 9 · 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 $$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.)

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

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

Show more comments
avatar image
9

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

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.

Follow this Question

Answers Answers and Comments

28 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 avatar image avatar image

Related Questions

[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

Cannot edit text file. 0 Answers

Trouble creating a text file 4 Answers

File.Create not working 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges