0

I am copying file from my server.map path to some folder in C:\ But When i am copying my file i want that it create the folder path same it is in server.map path with file to copy

Here is my code where i am copying file but it is not creating the same directory which i want.

  public void CopyFiles()
        {
            string Filename = "PSK_20150318_1143342198885.jpg";
            string sourcePath = Server.MapPath("~/UserFiles/Images/croppedAvatar/");
            string targetPath = @"C:\MyCopyFIle\";

        // Use Path class to manipulate file and directory paths.
        string sourceFile = System.IO.Path.Combine(sourcePath,Filename);
        string destFile = System.IO.Path.Combine(targetPath,Filename);

        // To copy a folder's contents to a new location:
        // Create a new target folder, if necessary.
        if (!System.IO.Directory.Exists(targetPath))
        {
            System.IO.Directory.CreateDirectory(targetPath);
        }

        // To copy a file to another location and 
        // overwrite the destination file if it already exists.
        System.IO.File.Copy(sourceFile, destFile, true);

        // To copy all the files in one directory to another directory.
        // Get the files in the source folder. (To recursively iterate through
        // all subfolders under the current directory, see
        // "How to: Iterate Through a Directory Tree.")
        // Note: Check for target path was performed previously
        //       in this code example.
        if (System.IO.Directory.Exists(sourcePath))
        {
            string[] files = System.IO.Directory.GetFiles(sourcePath);

            // Copy the files and overwrite destination files if they already exist.
            foreach (string s in files)
            {
                // Use static Path methods to extract only the file name from the path.
                Filename = System.IO.Path.GetFileName(s);
                destFile = System.IO.Path.Combine(targetPath);
                System.IO.File.Copy(s, destFile, true);
            }
        }
        else
        {
            Debug.WriteLine("Source path does not exist!");
        }

    }

Now the source path contain /userfiles/images/croppedavatar directory in it when I am copying it in c:\MyCopyFile I want it create a folder structure like c:\MyCopyFile\UserFile\Images\CroppedAvatar

Vojtěch Dohnal
  • 7,867
  • 3
  • 43
  • 105
Neeraj Mehta
  • 1,675
  • 2
  • 22
  • 45

3 Answers3

0

You can simply create a folder structure using FileInfo Class:

 string path=@"c:/MyCopyFile/UserFile/Images/CroppedAvatar/";
 FileInfo file = new FileInfo(path);
 file.Directory.Create();

This will create a directory where you can copy what you want to.

Kayani
  • 942
  • 7
  • 23
0

You can change your code like following

    if (System.IO.Directory.Exists(sourcePath))
    {
        string[] files = System.IO.Directory.GetFiles(sourcePath);

        // Copy the files and overwrite destination files if they already exist.
        foreach (string s in files)
        {
                Filename = System.IO.Path.GetFileName(s);
                destFile = System.IO.Path.Combine(targetPath, Filename);

                string path = @"C:/MyCopyFile/UserFiles/Images/croppedAvatar/";
                FileInfo file = new FileInfo(path);
                file.Directory.Create();

                string folderStructurePath = @"C:/MyCopyFile/UserFiles/Images/croppedAvatar/" + Filename;
                System.IO.File.Copy(sourceFile, folderStructurePath, true);
        }
    }
Golda
  • 3,823
  • 10
  • 34
  • 67
0

you have to check each file structure and create the same on destination and then copy file as

  public void CopyFiles(string srcpath, List<string> sourcefiles, string destpath)
    {


        DirectoryInfo dsourceinfo = new DirectoryInfo(srcpath);
        if (!Directory.Exists(destpath))
        {
            Directory.CreateDirectory(destpath);
        }
        DirectoryInfo dtargetinfo = new DirectoryInfo(destpath);
        List<FileInfo> fileinfo = sourcefiles.Select(s => new FileInfo(s)).ToList();
        foreach (var item in fileinfo)
        {

            string destNewPath = CreateDirectoryStructure(item.Directory.FullName, destpath) + "\\" + item.Name;
             File.Copy(item.FullName, destNewPath);
        }
    }
    public string CreateDirectoryStructure(string newPath, string destPath)
    {
        Queue<string> queue = new Queue<string>(newPath.Split(new string[] { "\\" }, StringSplitOptions.RemoveEmptyEntries));
        queue.Dequeue();
        while (queue.Count>0)
        {
            string dirName = queue.Dequeue();
            if(!new DirectoryInfo(destPath).GetDirectories().Any(a=>a.Name==dirName))
            {
                Directory.CreateDirectory(destPath + "\\" + dirName);
                destPath += "\\" + dirName;
            }
        }
        return destPath;

    }
Akshita
  • 849
  • 8
  • 15