Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create directory

Tags:

c#

directory

I need to create a directory, but, the directory when I need to create is inside of another directory. Something like this:

        Directory.CreateDirectory(@"teste\teste\teste\teste\");

basically, this directory does not exist ( of course ), but, the CreateDirectory(...) not support this string style, how I can make to create this directories ?

My way to make this is that:

    private void createdir(string _path)
    {
        string path = string.Empty;
        string[] dir = _path.Split('\\');

        for(int i=0;i<dir.Length;i++)
        {
            path += dir[i] + "\\";
            Directory.CreateDirectory(path);
        }
    }

But, I want to know, if have a more better way to make this ( a more legible ) more rapid.

like image 266
Alexandre Avatar asked Dec 04 '25 06:12

Alexandre


2 Answers

Directory.Create("c:\teste\teste\teste\teste"); should workt

like image 79
Retired_User Avatar answered Dec 06 '25 23:12

Retired_User


according to MSDN, you can nest the directory . CreateDirectory

 Directory.CreateDirectory("Public\\Html");
     Directory.CreateDirectory("\\Users\\User1\\Public\\Html");
     Directory.CreateDirectory("c:\\Users\\User1\\Public\\Html"); // using verbatim string you can escape slashes


if(System.IO.Directory.Exists(yourPath))
{
  Directory.CreateDirectory(yourPath);
}
like image 42
Ravi Gadag Avatar answered Dec 07 '25 00:12

Ravi Gadag



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!