Vasileios Pallas
Vasileios Pallas

Reputation: 4877

Create subdirectory, set name from string C#

I wonder how i can create a folder and the name of the folder is a value from a string.

This

    string[] directories = Directory.GetDirectories(filePath2);
    foreach (string directory in directories)
    {
         Directory.CreateDirectory(filePath + directory);                 
    }

or this

    string[] directories = Directory.GetDirectories(filePath2);
    foreach (string directory in directories)
    {
         Directory.CreateDirectory(filePath + @"/"+directory+"/");                 
    }

throws a NotSupportedException

How can I do it?

Upvotes: 0

Views: 665

Answers (4)

Sudhakar Tillapudi
Sudhakar Tillapudi

Reputation: 26209

Problem : if i understand correctly, you wanted to create the directories under the given path.

As others already stated it throws NotSupportedException when there is a colon : which is not part of the drive letter.

From MSDN : NotSupportedException

path contains a colon character (:) that is not part of a drive label ("C:\").

if you want to create a directory under the given path, you need to extract only the directory name(without drive path) from the directory variable which contains overall directory path including drive path which you does not need.

Solution: You need to extract only the DirectoryName from the directory variable in which it contains whole directory path (which also includes drive path- which is leading to Exception)

Try This:

foreach (string directory in directories)
{
  Directory.CreateDirectory(filePath + @"/" + 
                        directory.Substring(directory.LastIndexOf("\\")) + "/");

}

Upvotes: 1

JensG
JensG

Reputation: 13411

The MSDN documents the possible exceptions along with the error conditions.

NotSupportedException

path contains a colon character (:) that is not part of a drive label ("C:\").

Upvotes: 0

Melvinr
Melvinr

Reputation: 534

As the documentation says

NotSupportedException: path contains a colon character (:) that is not part of a drive label ("C:\").

Take a look http://msdn.microsoft.com/en-us/library/9h4z99zb(v=vs.110).aspx

Upvotes: 0

Saverio Terracciano
Saverio Terracciano

Reputation: 3915

According to the documentation on MSDN ( http://msdn.microsoft.com/en-us/library/54a0at6s(v=vs.110).aspx ) a NotSupportedException is raised when a "path contains a colon character (:) that is not part of a drive label ("C:\")." Check what is the content of your string.

Also in this regard remember:

  • Any and all directories specified in path are created, unless they already exist or unless some part of path is invalid.
  • If the directory already exists, this method does not create a new directory, but it returns a DirectoryInfo object for the existing directory.
  • The path parameter specifies a directory path, not a file path.
  • Trailing spaces are removed from the end of the path parameter before creating the directory.
  • You can create a directory on a remote computer, on a share that you have write access to. UNC paths are supported; for example, you can specify the following for path: \2009\Archives\December in Visual Basic, and \\2009\Archives\December in C#.
  • Creating a directory with only the colon character (:) is not supported, and will cause a NotSupportedException to be thrown.

Upvotes: 2

Related Questions