Register for your free account! | Forgot your password?

Go Back   elitepvpers > Coders Den > .NET Languages
You last visited: Today at 01:15

  • Please register to post and access all features, it's quick, easy and FREE!

Advertisement



[C#]Copying specific folder using c#

Discussion on [C#]Copying specific folder using c# within the .NET Languages forum part of the Coders Den category.

Reply
 
Old   #1
 
elite*gold: 0
Join Date: May 2009
Posts: 67
Received Thanks: 2
Smile [C#]Copying specific folder using c#

Hi there fellow programmers i'm having trouble figuring out how to copy 3 specific folders into one directory.
1 this codes cant copy directory
================================================== ==
DirectoryInfo Fold1 = new DirectoryInfo(@"C:\FolderB\201607061");
DirectoryInfo Fold2 = new DirectoryInfo(@"C:\FolderB\201607062");

private void btnPhotosCopy_Click(object sender, EventArgs e)
{
Fold1.CopyTo(@"E:\Photos\201607061");
Fold1.CopyTo(@"E:\Photos\201607062");
}
================================================== ==

2 this code can copy directory but only one
================================================== ==
public partial class mainForm : Form
{
const string FromDir = "C:/FolderA/";
const string FromDir2 = "C:/FolderB/";
const string ToDir = "D:/USBTest/";

public mainForm()
{
InitializeComponent();
}

private void btnCopy_Click(object sender, EventArgs e)
{


DirectoryInfo dirCopyFrom = new DirectoryInfo(FromDir2);
FileInfo[] diskFiles = dirCopyFrom.GetFiles(FromDir2);
foreach (FileInfo curFile in diskFiles) <--- I cant make 2 foreach loop
{
try
{
File.Copy(curFile.FullName, ToDir + curFile.Name, true);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}

}
================================================== ==
Please help
mauripan is offline  
Old 07/18/2016, 15:05   #2
 
YatoDev's Avatar
 
elite*gold: 50
Join Date: Sep 2012
Posts: 3,841
Received Thanks: 1,462
why not

Code:
using System.IO;

foreach(string file in Directory.GetFiles(path)
{
      File.Copy(file, Path.Combine(newPath, Path.GetFileName(file)));
}
and this for each directory?
YatoDev is offline  
Old 07/18/2016, 19:34   #3
 
elite*gold: 0
Join Date: May 2009
Posts: 67
Received Thanks: 2
uhh... I still didn't figure it out. My codes are not working. Still I cant copy FolderA in drive:C to drive: D
mauripan is offline  
Old 07/18/2016, 19:40   #4
 
.Scy's Avatar
 
elite*gold: 15
Join Date: Jul 2010
Posts: 3,926
Received Thanks: 1,158


microsoft can explain that better than i can.

that should be what you want.
.Scy is offline  
Old 07/19/2016, 10:36   #5
 
elite*gold: 0
Join Date: May 2009
Posts: 67
Received Thanks: 2
Hi Master .Scy can you give example on copying the whole folder from drive c: to drive d: please....
mauripan is offline  
Old 07/19/2016, 13:01   #6
 
.Scy's Avatar
 
elite*gold: 15
Join Date: Jul 2010
Posts: 3,926
Received Thanks: 1,158
This is the code that site provides.
Code:
using System;
using System.IO;

class DirectoryCopyExample
{
    static void Main()
    {
        // Copy from the current directory, include subdirectories.
        DirectoryCopy(".", @".\temp", true);
    }

    private static void DirectoryCopy(string sourceDirName, string destDirName, bool copySubDirs)
    {
        // Get the subdirectories for the specified directory.
        DirectoryInfo dir = new DirectoryInfo(sourceDirName);

        if (!dir.Exists)
        {
            throw new DirectoryNotFoundException(
                "Source directory does not exist or could not be found: "
                + sourceDirName);
        }

        DirectoryInfo[] dirs = dir.GetDirectories();
        // If the destination directory doesn't exist, create it.
        if (!Directory.Exists(destDirName))
        {
            Directory.CreateDirectory(destDirName);
        }

        // Get the files in the directory and copy them to the new location.
        FileInfo[] files = dir.GetFiles();
        foreach (FileInfo file in files)
        {
            string temppath = Path.Combine(destDirName, file.Name);
            file.CopyTo(temppath, false);
        }

        // If copying subdirectories, copy them and their contents to new location.
        if (copySubDirs)
        {
            foreach (DirectoryInfo subdir in dirs)
            {
                string temppath = Path.Combine(destDirName, subdir.Name);
                DirectoryCopy(subdir.FullName, temppath, copySubDirs);
            }
        }
    }
}
if you have any knowledge of c# you will know how to call that function with the right parameters, no need for me to write your code for you.

this code-snippet even offers an example of use
.Scy is offline  
Reply


Similar Threads Similar Threads
[Rsro] Fully working ibot folder with folder + tut
05/06/2011 - SRO Hacks, Bots, Cheats & Exploits - 45 Replies
Ok, this is the first time im making a tutorial so dont be harsh on the comments. Download link: MEGAUPLOAD - The leading online storage and file delivery service Mirror:http://hotfile.com/dl/83017772/eb1dacc/rsr obot.7z.html PS: you need 7zip + i accidently copied 2 ibot_necessities inside it, so just delete one of them. PPS: ive realised that there are options for ma chars too so, u can delete them if you want :D



All times are GMT +1. The time now is 01:16.


Powered by vBulletin®
Copyright ©2000 - 2026, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Support | Contact Us | FAQ | Advertising | Privacy Policy | Terms of Service | Abuse
Copyright ©2026 elitepvpers All Rights Reserved.