Reputation: 1011
I am working on an asp.net application where i have to move any Folder and its contents to another. Suppose i have a Main folder and in that folder there is 3 subfolders. In each subfolder there is a file. I want to move folder and all its contents to another place. For this i used the following code
if (!Directory.Exists(@"E:\Sunny\C#FolderCopy"))
{
Directory.CreateDirectory(@"E:\Sunny\C#FolderCopy");
Directory.Move(@"E:\Sunny\C#Folder\", @"E:\Sunny\C#FolderCopy\");
}
But when the control reaches to move function The error comes as
Cannot create a file when that file already exists.
How to solve this
Upvotes: 2
Views: 8408
Reputation: 8099
Directory.Move
already creates the folder for you. You only need to adjust to the following:
if (!Directory.Exists(@"E:\Sunny\C#FolderCopy"))
{
Directory.Move(@"E:\Sunny\C#Folder\", @"E:\Sunny\C#FolderCopy\");
}
If you want to copy the folder (as indicated by your comment) you can use FileSystem.CopyDirectory
. It is located in a Visual Basic namespace but that should be of no concern at all:
using Microsoft.VisualBasic.FileIO;
if (!Directory.Exists(@"E:\Sunny\C#FolderCopy"))
{
FileSystem.CopyDirectory(@"E:\Sunny\C#Folder\", @"E:\Sunny\C#FolderCopy\");
}
Or use this method (taken from msdn):
DirectoryCopy(".", @".\temp", true);
private static void DirectoryCopy(
string sourceDirName, string destDirName, bool copySubDirs)
{
DirectoryInfo dir = new DirectoryInfo(sourceDirName);
DirectoryInfo[] dirs = dir.GetDirectories();
// If the source directory does not exist, throw an exception.
if (!dir.Exists)
{
throw new DirectoryNotFoundException(
"Source directory does not exist or could not be found: "
+ sourceDirName);
}
// If the destination directory does not exist, create it.
if (!Directory.Exists(destDirName))
{
Directory.CreateDirectory(destDirName);
}
// Get the file contents of the directory to copy.
FileInfo[] files = dir.GetFiles();
foreach (FileInfo file in files)
{
// Create the path to the new copy of the file.
string temppath = Path.Combine(destDirName, file.Name);
// Copy the file.
file.CopyTo(temppath, false);
}
// If copySubDirs is true, copy the subdirectories.
if (copySubDirs)
{
foreach (DirectoryInfo subdir in dirs)
{
// Create the subdirectory.
string temppath = Path.Combine(destDirName, subdir.Name);
// Copy the subdirectories.
DirectoryCopy(subdir.FullName, temppath, copySubDirs);
}
}
}
Upvotes: 4
Reputation: 675
Recursive Files Move. Call this method and all it do.
public static void MoveDirectory(string source, string target)
{
var sourcePath = source.TrimEnd('\\', ' ');
var targetPath = target.TrimEnd('\\', ' ');
var files = Directory.EnumerateFiles(sourcePath, "*", SearchOption.AllDirectories)
.GroupBy(s=> Path.GetDirectoryName(s));
foreach (var folder in files)
{
var targetFolder = folder.Key.Replace(sourcePath, targetPath);
Directory.CreateDirectory(targetFolder);
foreach (var file in folder)
{
var targetFile = Path.Combine(targetFolder, Path.GetFileName(file));
if (File.Exists(targetFile)) File.Delete(targetFile);
File.Move(file, targetFile);
}
}
Directory.Delete(source, true);
}
Upvotes: 2