views:

1809

answers:

2

I need a simple function which will take a FileInfo and a destination_directory_name as input, get the file path from the fileinfo and replicate it in the destination_directory_name passed as the second parameter.

for ex. filepath is "d:\recordings\location1\client1\job1\file1.ext the function should create the directories in the destination_directory_name if they dont exist and copy the file after creating the directories.

+5  A: 

I'm using the following method for that purpose:

public static void CreateDirectory(DirectoryInfo directory)
{
    if (!directory.Parent.Exists)
     CreateDirectory(directory.Parent);
    directory.Create();
}

Use it in this way:

// path is your file path
string directory = Path.GetDirectoryName(path);
CreateDirectory(new DirectoryInfo(directory));
M4N
lovely, so simple too!
Calanus
Thanks for the answer. Even i wanted this for one of my application
Ravisha
+3  A: 

Hi, Directory.CreateDirectory can be used to create the final directory and will automatically create all folders in the path if they don't exist.

Thx

Andy, NZ

I think if you put some more detail in this, just a simple code example and a link to the MSDN docs, and removed the extraneous sign-off, it would make more sense for this to be the accepted answer.
Daniel Earwicker
This seems to be very straightforward to understand. Why there is a need for an example. Please see: http://msdn.microsoft.com/en-us/library/system.io.directory.createdirectory(VS.71).aspx
Samuel