Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

creating files, recursively creating directories

Tags:

c#

I was reading some file IO tutorials for C# and have managed to write out some files, but what if the path I'm given contains directories?

For example, I want to create the file called data/my_file except data folder doesn't exist.

The line,

BinaryWriter outFile = new BinaryWriter(File.OpenWrite(path)); 

where path is the above string, crashes with the message that part of the path doesn't exist, which means C# isn't creating them as required.

I would like C# to handle all of the messy directory creation and checking for me instead of me having to parse the path and create all of the necessary directories. Is this possible? Otherwise, is there a snippet of code that I can just copy over into my project which will handle anything I might be overlooking (since I don't know much about file management).

like image 816
MxLDevs Avatar asked Jun 08 '12 00:06

MxLDevs


People also ask

How do I create a recursive directory?

The -p option is used to create multiple child directories with mkdir command in a recursive manner. In order to create directories recursively non of the specified directories should exist because all of them are created automatically.

What are recursive directories?

What is a recursive listing of files? Recursive means that Linux or Unix command works with the contains of directories, and if a directory has subdirectories and files, the command works on those files too (recursively).

How do I make a directory recursive in Python?

makedirs() method in Python is used to create a directory recursively. That means while making leaf directory if any intermediate-level directory is missing, os. makedirs() method will create them all. Suppose we want to create directory 'ihritik' but Directory 'GeeksForGeeks' and 'Authors' are unavailable in the path.


1 Answers

System.IO.Directory.CreateDirectory() will create all directories and subdirectories in a specified path, should they not already exist.

You can call it, passing the path, to ensure the folder structure is created prior to writing your file.

like image 183
Eric J. Avatar answered Sep 24 '22 09:09

Eric J.