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. How to copy a file?

How to copy a file?

Scheduled Pinned Locked Moved C#
tutorialquestion
5 Posts 4 Posters 5 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.
  • M Offline
    M Offline
    masoudshao
    wrote on last edited by
    #1

    Hi, I need to write a program which can copy a file simply from a source folder to a destination folder. Please include the codes for a simple program doing so.

    A A 2 Replies Last reply
    0
    • M masoudshao

      Hi, I need to write a program which can copy a file simply from a source folder to a destination folder. Please include the codes for a simple program doing so.

      A Offline
      A Offline
      Abhijit Jana
      wrote on last edited by
      #2

      Below is the sample VB.NET Code. Hope you can convert it easily using C#.

      using System.IO

      Public Sub CopyDir(ByVal strSrc As String, ByVal strDest As String)
      Dim dirInfo As New DirectoryInfo(strSrc)
      Dim fsInfo As FileSystemInfo
      If Not Directory.Exists(strDest) Then
      Directory.CreateDirectory(strDest)
      End If
      For Each fsInfo In dirInfo.GetFileSystemInfos
      Dim strDestFileName As String = Path.Combine(strDest, fsInfo.Name)
      If TypeOf fsInfo Is FileInfo Then
      File.Copy(fsInfo.FullName, strDestFileName, True)
      'This will overwrite files that already exist
      Else
      CopyDir(fsInfo.FullName, strDestFileName)
      End If
      Next
      End Sub

      Source : http://abhijitjana.blogspot.com/2007/10/copy-files-from-one-directory-to.html[^]

      Abhijit Jana | Codeproject MVP Web Site : abhijitjana.net Don't forget to click "Good Answer" on the post(s) that helped you.

      A 1 Reply Last reply
      0
      • M masoudshao

        Hi, I need to write a program which can copy a file simply from a source folder to a destination folder. Please include the codes for a simple program doing so.

        A Offline
        A Offline
        Abhishek Sur
        wrote on last edited by
        #3

        use System.IO.File.Copy(src,dest) where src and dest are source and target respectively. :) :)

        Abhishek Sur **Don't forget to click "Good Answer" if you like this Solution.


        My Latest Articles-->** Microsoft Bing MAP using Javascript
        CLR objects in SQL Server 2005
        Uncommon C# Keywords
        /xml>

        1 Reply Last reply
        0
        • A Abhijit Jana

          Below is the sample VB.NET Code. Hope you can convert it easily using C#.

          using System.IO

          Public Sub CopyDir(ByVal strSrc As String, ByVal strDest As String)
          Dim dirInfo As New DirectoryInfo(strSrc)
          Dim fsInfo As FileSystemInfo
          If Not Directory.Exists(strDest) Then
          Directory.CreateDirectory(strDest)
          End If
          For Each fsInfo In dirInfo.GetFileSystemInfos
          Dim strDestFileName As String = Path.Combine(strDest, fsInfo.Name)
          If TypeOf fsInfo Is FileInfo Then
          File.Copy(fsInfo.FullName, strDestFileName, True)
          'This will overwrite files that already exist
          Else
          CopyDir(fsInfo.FullName, strDestFileName)
          End If
          Next
          End Sub

          Source : http://abhijitjana.blogspot.com/2007/10/copy-files-from-one-directory-to.html[^]

          Abhijit Jana | Codeproject MVP Web Site : abhijitjana.net Don't forget to click "Good Answer" on the post(s) that helped you.

          A Offline
          A Offline
          Ashfield
          wrote on last edited by
          #4

          If you keep writing their code for them the will pass the exams, and one day you may end you having to mantain some of their code - even worse, I may end up having to fix it :) Give a hint, but please refrain from giving code for something as trivial as this otherwise they will never learn that one of the more important aspects of development is learning how to work things out for yourself. I mean, how difficult is it to go to google and put in file copy c# and look at the top few of the 815,000 hits.

          Bob Ashfield Consultants Ltd Proud to be a 2009 Code Project MVP

          A 1 Reply Last reply
          0
          • A Ashfield

            If you keep writing their code for them the will pass the exams, and one day you may end you having to mantain some of their code - even worse, I may end up having to fix it :) Give a hint, but please refrain from giving code for something as trivial as this otherwise they will never learn that one of the more important aspects of development is learning how to work things out for yourself. I mean, how difficult is it to go to google and put in file copy c# and look at the top few of the 815,000 hits.

            Bob Ashfield Consultants Ltd Proud to be a 2009 Code Project MVP

            A Offline
            A Offline
            Abhijit Jana
            wrote on last edited by
            #5

            Ashfield wrote:

            Give a hint,

            Most of the cases I did the same. Just give the hint so that they can take it forward.

            Ashfield wrote:

            how difficult is it to go to google and put in file copy c# and look at the top few of the 815,000 hits.

            I do the same ;) and Got the link from my Old Blog and I put it over here.

            Abhijit Jana | Codeproject MVP Web Site : abhijitjana.net Don't forget to click "Good Answer" on the post(s) that helped you.

            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