2

I have to create multiple folders as e.g.

Directory.CreateDirectory("PATH\\" + _year + "filetosave.txt");  

while "PATH\\" is the full path where the folder will reside, _year is the parameter and "filetosave.txt" is the file which is to be saved in respective folder.
And at run time, it should create respective folders with years in the folder name containing respective files to save.

Whereas .CreateDirectory() method only accepts string path or string path, security access as parameters.

  • How will we create these parameterized folders?
  • How can we make a check that a specified directory already exists or not?
maliks
  • 1,102
  • 3
  • 18
  • 42
  • 3
    In general, use `System.IO.Path.Combine` to build paths. – Tim Schmelter May 23 '16 at 15:30
  • `Directory.CreateDirectory(Path.Combine("Path\\", DateTime.Now.Year, "filetosave.txt"));` ? – Guillaume Beauvois May 23 '16 at 15:32
  • See this thread for creating a method that will create the directory and file at the same time. http://stackoverflow.com/questions/3201598/how-do-i-create-a-file-and-any-folders-if-the-folders-dont-exist If you want to create for multiple years, put this in a foreach look or use a Linq lambda. – Murray Foxcroft May 23 '16 at 15:33

3 Answers3

3
var path = Path.Combine("PATH\\", _year.ToString(), "filettosave.txt");
Directory.CreateDirectory(path);

Directory.CreateDirectory

Creates all directories and subdirectories in the specified path unless they already exist.

Emphasis mine.

Lews Therin
  • 3,707
  • 2
  • 27
  • 53
  • @Sylverac--I think as you suggested `Path.Combine()` method, the parameter `_year` should be `_year.ToString()` as it only accepts stirngs – maliks May 23 '16 at 15:52
2

As commented, use Path.Combine when trying to build system paths:

var root = "Path";
var year = "2016";
var filename = "filetosave.txt";

var path = Path.Combine(root, year, filename);
//  path = Path\2016\filetosave.txt

Directory.CreateDirectory(path);
Jonesopolis
  • 25,034
  • 12
  • 68
  • 112
  • @Jonesopolis--if the years are passed in a loop, then how will we handle? As years are not to be defined manually each time! – maliks May 23 '16 at 15:38
  • i dont understand how that causes any sort of problem. You'd just loop over your years and create a directory for each one. This was meant to be an example of building a dynamic path. – Jonesopolis May 23 '16 at 15:41
  • Right its mean the `var = year` if passed as a parameter in a loop, it'll work fine – maliks May 23 '16 at 15:42
  • @Jonesopolis--`Path.Combine()` method has some invalid arguments. The error raised. – maliks May 23 '16 at 15:48
  • You're probably passing an `int year` as an argument. It takes strings. I'm sure you can figure it out. – Jonesopolis May 23 '16 at 15:50
  • Yeah obviously, I just changed it to `_year.ToString()`, now it worked – maliks May 23 '16 at 15:51
  • So, what for another question I just added that how can we have a check that specified directory already exists or not? – maliks May 23 '16 at 15:58
  • @Jonesopolis--How can we write text in these parameterized folders? – maliks May 31 '16 at 12:20
0

In general, use System.IO.Path.Combine to build paths. It simplifies this task.

string dir = System.IO.Path.Combine(rootPath, _year.ToString());
Directory.CreateDirectory(dir);
string file =  System.IO.Path.Combine(dir, "filetosave.txt");
FileStream fs = File.Create(file)
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939