Reputation: 67213
If I have a path, like so:
c:\windows\system32\inetsrv\abc.exe
And I want to make a folder under a drive/path such as
Z:\Stuff\
How can I make a sub directory and select whether I want everything from windows onwards, system32 onwards, or inetsrv onwards? BTW I know DirectoryInfo
has a method called CreateSubDirectory()
.
Thanks
Upvotes: 0
Views: 459
Reputation: 244757
If you want to copy everything from one directory to another you can use something like this:
using System;
using System.Collections.Generic;
using System.IO;
namespace so_cp__r
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter path from where you want to copy:");
List<DirectoryInfo> fromDirs = new List<DirectoryInfo>();
DirectoryInfo dir = new FileInfo(Console.ReadLine()).Directory;
while (dir != null)
{
fromDirs.Add(dir);
dir = dir.Parent;
}
for (int i = 0; i < fromDirs.Count; i++)
Console.WriteLine("{0}: {1}", i, fromDirs[i].FullName);
Console.WriteLine("From which of these dirs do you want to start?");
DirectoryInfo fromDir = fromDirs[int.Parse(Console.ReadLine())];
Console.WriteLine("Where do you want to copy?");
DirectoryInfo toDir = new DirectoryInfo(Console.ReadLine());
recursive_copy(fromDir, toDir);
}
static void recursive_copy(DirectoryInfo fromDir, DirectoryInfo toDir)
{
DirectoryInfo child = toDir.CreateSubdirectory(fromDir.Name);
foreach (FileInfo file in fromDir.GetFiles())
file.CopyTo(child.FullName + Path.DirectorySeparatorChar + file.Name);
foreach (DirectoryInfo dir in fromDir.GetDirectories())
recursive_copy(dir, child);
}
}
}
Upvotes: 1
Reputation: 2955
You can use System.IO.Directory.CreateDirectory(path). It is a static method unlike the DirectoryInfo one where you need an instance of a directory to call it.
After that you could use Xcopy or Robocopy to copy all the files and subdirectories.
Upvotes: 2