Requirement:
I need to copy a folder/directory and its contents located in a Machine A to Machine B successfully.
Before starting to copy the following points needs to be consider for my requirement.
-
If the destination machine,Destination folder is having access permissions or not for the user which he needs to copy from the source folder or directory.
-
Destination directory should NOT be in hidden or shared and it should be empty if it is already exists.
-
The destination machine expects the credentials for access,to Handle the same accordingly
How to acheive this?
I’m unable to acheive with the below code:
using System;
using System.IO;
class DirectoryCopyExample
{
static void Main()
{
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);
}
}
}
}
For file sending you can see your OWN question here:
Machine to Machine File transfer
AND some short script here:
http://www.eggheadcafe.com/community/aspnet/2/10076226/file-transfer-from-one-ma.aspx
Than, using FileInfo and DirectoryInfo – get the attributes:
http://msdn.microsoft.com/en-us/library/system.io.directoryinfo%28VS.71%29.aspx
Connecting to the remote folder can be done using DirectorySecurity:
http://msdn.microsoft.com/en-us/library/system.security.accesscontrol.directorysecurity.aspx
Enjoy!