Solved

File Copy

Posted on 2010-11-29
4
384 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
  • 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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
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…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

760 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now