views:

66

answers:

4

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.

  1. 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.

  2. Destination directory should NOT be in hidden or shared and it should be empty if it is already exists.

  3. 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);
        }
    }
 }
}
+2  A: 

you have all this information in the DirectoryInfo of your DirectoryInfo dirInfo = new DirectoryInfo(path);

serhio
A: 

Check out the System.Security.AccessControl.DirectorySecurity class You can get the DirectorySecurity object by calling: System.IO.Directory.GetAccessControl("DIR_NAME") I am not sure if this can get you information on a directory on remote machine.

A9S6
+1  A: 

For file sending you can see your OWN question here: http://stackoverflow.com/questions/1973093/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!

yn2
+2  A: 

Your code snippet doesn't really copy files between two machines, instead within the same machine. Which is obvious from your Main method.

If you wanted to transfer between machines and that too to an unshared location, you should probably check sockets.

If it is a shared location with appropriate permissions set, then File.copy would do the help for you.

ArunDhaJ