Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Need to copy folder from one path to other

Need to copy folder from one path to other

Scheduled Pinned Locked Moved C#
help
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    honeyashu
    wrote on last edited by
    #1

    I have to find a folder from the path where my application exist e.g. c:/programfile/RTGSL/exe/.. then from exe folder i have to copy two folders(with all data) name applicationdata & client from this path to the current logged in user's personal folder path i.e. C:\Documents and Settings\ashishjaiswal\Application Data\clientdata\ if and only if no folder exist at this path.. Can any body help me with this code .. I am really new to programming

    M 1 Reply Last reply
    0
    • H honeyashu

      I have to find a folder from the path where my application exist e.g. c:/programfile/RTGSL/exe/.. then from exe folder i have to copy two folders(with all data) name applicationdata & client from this path to the current logged in user's personal folder path i.e. C:\Documents and Settings\ashishjaiswal\Application Data\clientdata\ if and only if no folder exist at this path.. Can any body help me with this code .. I am really new to programming

      M Offline
      M Offline
      musefan
      wrote on last edited by
      #2

      OK, this one is not as easy as you would like to think. As far as I know you need to do all the work here. Basically, you should first obtain you two folder paths. Application path can be obtained using...

      string sourceDir = Application.StartupPath + "\\FolderToCopy";

      Then you destination path should be available in the System.Environment.SpecialFolder enum, im not sure which one you will want to use thou so either test a few or google for answers...

      string destDir = System.Environment.SpecialFolder.ApplicationData;//I guess this one is right

      Now you need to create a recursive function that will copy a folder and all sub-folders and files. Keep in mind this is off top of my head so sorry if it does not work as it should...

      void CopyFolder(string source, string destination)
      {
      System.IO.DirectoryInfo info = new System.IO.DirectoryInfo(source);//get info for the folder
      System.IO.Directory.CreateDirectory(destination);//create the folder

      foreach(System.IO.FileInfo file in info.GetFiles())
      {
      file.CopyTo(destination + "\\" + file.Name);
      }

      foreach(System.IO.DirectoryInfo dir in info.GetDirectories())
      {
      CopyFolder(dir.FullName, destination + "\\" + dir.Name);
      }

      }

      ..sorry if it does'nt work properly, but im sure you can google for 'C# Copy Directory' and get the code you need

      Life goes very fast. Tomorrow, today is already yesterday.

      M 1 Reply Last reply
      0
      • M musefan

        OK, this one is not as easy as you would like to think. As far as I know you need to do all the work here. Basically, you should first obtain you two folder paths. Application path can be obtained using...

        string sourceDir = Application.StartupPath + "\\FolderToCopy";

        Then you destination path should be available in the System.Environment.SpecialFolder enum, im not sure which one you will want to use thou so either test a few or google for answers...

        string destDir = System.Environment.SpecialFolder.ApplicationData;//I guess this one is right

        Now you need to create a recursive function that will copy a folder and all sub-folders and files. Keep in mind this is off top of my head so sorry if it does not work as it should...

        void CopyFolder(string source, string destination)
        {
        System.IO.DirectoryInfo info = new System.IO.DirectoryInfo(source);//get info for the folder
        System.IO.Directory.CreateDirectory(destination);//create the folder

        foreach(System.IO.FileInfo file in info.GetFiles())
        {
        file.CopyTo(destination + "\\" + file.Name);
        }

        foreach(System.IO.DirectoryInfo dir in info.GetDirectories())
        {
        CopyFolder(dir.FullName, destination + "\\" + dir.Name);
        }

        }

        ..sorry if it does'nt work properly, but im sure you can google for 'C# Copy Directory' and get the code you need

        Life goes very fast. Tomorrow, today is already yesterday.

        M Offline
        M Offline
        Member 1033907
        wrote on last edited by
        #3

        musefan wrote:

        string destDir = System.Environment.SpecialFolder.ApplicationData;//I guess this one is right

        Actually, it's

        string destDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups