Solved

File Copy

Posted on 2010-11-29
4
388 Views
Last Modified: 2012-05-10
I have the following function to copy the file

public static void CopyFile(string source, string destination, string filedirectory,
                                    int bufferSize)
        {
            if (!string.IsNullOrEmpty(filedirectory)) // See if directory exists. If not create it.
            {
                if (!Directory.Exists(filedirectory))
                {
                    Directory.CreateDirectory(filedirectory);
                }
            }
            using (FileStream outputFile = File.OpenWrite(destination))
            {
                using (FileStream inputFile = File.OpenRead(source))
                {
                    var buffer = new byte[bufferSize];
                    int bytesRead;
                    while ((bytesRead = inputFile.Read(buffer, 0, bufferSize)) != 0)
                    {
                        outputFile.Write(buffer, 0, bytesRead);
                    }
                }
            }
        }

Problem:
I have a text file in source and destination. The code is supposed to copy the file from source to destination. But what is happening is, it is appending the text in text file, instead of copying the file. I am surprised. Please help
0
Comment
Question by:GRChandrashekar
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 10

Expert Comment

by:hosneylk
ID: 34236773
why not use System.IO.File.Copy?
0
 
LVL 10

Accepted Solution

by:
hosneylk earned 500 total points
ID: 34236790
or if you want to stick to this change it like this


FileStream fileStream = new FileStream(@"c:\file.txt", FileMode.Create);

public static void CopyFile(string source, string destination, string filedirectory,
                                    int bufferSize)
        {
            if (!string.IsNullOrEmpty(filedirectory)) // See if directory exists. If not create it.
            {
                if (!Directory.Exists(filedirectory))
                {
                    Directory.CreateDirectory(filedirectory);
                }
            }
            using (FileStream outputFile = new FileStream(destination, FileMode.Create))
            {
                using (FileStream inputFile = File.OpenRead(source))
                {
                    var buffer = new byte[bufferSize];
                    int bytesRead;
                    while ((bytesRead = inputFile.Read(buffer, 0, bufferSize)) != 0)
                    {
                        outputFile.Write(buffer, 0, bytesRead);
                    }
                }
            }
        }

Open in new window

0
 

Author Comment

by:GRChandrashekar
ID: 34236840
Well textfile is only example !

How do i alter this code to always create mode [overwrite if exist] so that all the files being copied do not come across same issue
0
 
LVL 10

Expert Comment

by:hosneylk
ID: 34236843
did you try the code above? ignore the first line..
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question